Skip to main content
Tweeted twitter.com/StackSoftEng/status/1269418825201704962
Became Hot Network Question

I have this NamePath class that accepts a string from some system path and tries to split it tointo two properties, Name and LastName.

Is this firstthe code example below fine? I've read that throwing exceptions in constructor is okay, but is there a better way for this case?

public class NamePath{ public string Name {get; private set; } public string LastName { get; private set; } public NamePath(string path){ Parse(path); } private void Parse(string path){ if ( ParsingOkay() ) { // set the properties } else { throw new Exception } } } 

I've thought of another approach where NamePath is just data class, and I would have a static method on another class that would try to create NamePath object or else it would return null.

public class NamePath { public string Name {get; private set; } public string LastName { get; private set; } public NamePath (string name, string lastName){ Name = name; LastName = lastName; } } public class PathHelper { public static NamePath ParseNamePath (string path){ if ( ParsingOkay() ){ return new NamePath } else { return null } } } 

code example is semi-pseudo

I have this NamePath class that accepts a string from some system path and tries to split it to two properties Name and LastName.

Is this first code example fine? I've read that throwing exceptions in constructor is okay, but is there a better way for this case?

public class NamePath{ public string Name {get; private set; } public string LastName { get; private set; } public NamePath(string path){ Parse(path); } private void Parse(string path){ if ( ParsingOkay() ) { // set the properties } else { throw new Exception } } } 

I've thought of another approach where NamePath is just data class, and I would have a static method on another class that would try to create NamePath object or else it would return null.

public class NamePath { public string Name {get; private set; } public string LastName { get; private set; } public NamePath (string name, string lastName){ Name = name; LastName = lastName; } } public class PathHelper { public static NamePath ParseNamePath (string path){ if ( ParsingOkay() ){ return new NamePath } else { return null } } } 

code example is semi-pseudo

I have this NamePath class that accepts a string from some system path and tries to split it into two properties, Name and LastName.

Is the code example below fine? I've read that throwing exceptions in constructor is okay, but is there a better way for this case?

public class NamePath{ public string Name {get; private set; } public string LastName { get; private set; } public NamePath(string path){ Parse(path); } private void Parse(string path){ if ( ParsingOkay() ) { // set the properties } else { throw new Exception } } } 

I've thought of another approach where NamePath is just data class, and I would have a static method on another class that would try to create NamePath object or else it would return null.

public class NamePath { public string Name {get; private set; } public string LastName { get; private set; } public NamePath (string name, string lastName){ Name = name; LastName = lastName; } } public class PathHelper { public static NamePath ParseNamePath (string path){ if ( ParsingOkay() ){ return new NamePath } else { return null } } } 

code example is semi-pseudo

Source Link
jovcem
  • 113
  • 1
  • 1
  • 5

Throwing exception from constructor?

I have this NamePath class that accepts a string from some system path and tries to split it to two properties Name and LastName.

Is this first code example fine? I've read that throwing exceptions in constructor is okay, but is there a better way for this case?

public class NamePath{ public string Name {get; private set; } public string LastName { get; private set; } public NamePath(string path){ Parse(path); } private void Parse(string path){ if ( ParsingOkay() ) { // set the properties } else { throw new Exception } } } 

I've thought of another approach where NamePath is just data class, and I would have a static method on another class that would try to create NamePath object or else it would return null.

public class NamePath { public string Name {get; private set; } public string LastName { get; private set; } public NamePath (string name, string lastName){ Name = name; LastName = lastName; } } public class PathHelper { public static NamePath ParseNamePath (string path){ if ( ParsingOkay() ){ return new NamePath } else { return null } } } 

code example is semi-pseudo