I created a class dubPrime that takes an integer that must be a double digit. If it isn't a double digit the program is supposed to throw an exception. However my program crashes and outputs "[ERROR] FATAL UNHANDLED EXCEPTION" whenever I try to create an object that is not a double digit. *I used the factory design pattern so that the object wouldn't be created if it doesn't meet the standards.
Is there a way to circumvent this so that my program doesn't crash?
private DubPrime(bool upArg, bool enabledArg, uint xArg){
x = xArg;
up = upArg;
enabled = enabledArg;
}
public static DubPrime GetDubPrime(bool upArg = true, bool enabledArg = true, uint xArg = lowerLimit){
//x must be double digit
if(xArg<lowerLimit || xArg>upperLimit)
throw new ArgumentException("x should be a double digit");
else{
return new DubPrime(upArg, enabledArg, xArg);
}
}
Update: Whenever I try to add a try/catch block the compiler says "not all code paths return a value." Is it necessary to return something? Could I throw an exception and not have it return something?
public static DubPrime GetDubPrime(bool upArg = true, bool enabledArg = true, uint xArg = lowerLimit){
try{
if(xArg<lowerLimit || xArg>upperLimit)
throw new ArgumentException("x should be a double digit");
else{
return new DubPrime(upArg, enabledArg, xArg);
}
}
catch(Exception e){
Console.WriteLine(e.Message);
}
}