-1

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);
        }
    }
  • This is the code that throws the exception, but where is the code that catches it? – dxiv Apr 02 '21 at 23:04
  • Whenever I try to add a try/catch block it requires me to return something and won't compile. (Updated the question) – Kevin Khong Apr 02 '21 at 23:23
  • 2
    @KevinKhong Normally you would catch the exception in the function that calls `GetDubPrime`. Inside `GetDubPrime` itself, you can just handle the error case directly, with an `if/else` block for example. There is no need to throw an exception just to catch it a few lines later. – dxiv Apr 02 '21 at 23:26
  • If you throw an exception and don't catch it, the program terminates. So, catch the exception. See duplicate. For your other problem **please search the site for the exact error message**. That particular error is already thoroughly covered here. If after researching the error you still can't figure it out, post a new question making clear what research you've done and what _specifically_ you can't figure out. – Peter Duniho Apr 03 '21 at 06:51
  • 1
    See also https://stackoverflow.com/questions/505471/how-often-should-i-use-try-and-catch-in-c. You can write a factory method that follows the `Try...()` pattern, i.e. returns a `bool` value, and stores a success result in an `out` parameter, rather than throwing an exception. Alternatively, if you insist on throwing an exception, the factory method isn't helpful; you might as well throw the exception from the constructor of the type, because that will work just as well to prevent an invalid object from being returned. – Peter Duniho Apr 03 '21 at 06:59

1 Answers1

1

You need to outside of GetDubPrime catch the ArgumentException that you throw inside it. If nothing catches the exception the program exits.

Something like:

(...)
try 
{
   x = GetDubPrime(...);
} 
catch(ArgumentException ex) 
{
  // bad data, do something
  Console.WriteLine(ex.Message);
  x = 0; // or whatever is necessary
}
(...)
tymtam
  • 31,798
  • 8
  • 86
  • 126