Question

I have the following code in the 'main' method:

static void Main(string[] args)
{
    try
    {
         int a = 0;
         int b = 5;
         b /= a;
    }
    catch (MyException ex)
    {
         Console.WriteLine(ex.Message)
    }
}

And MyException class is as the following:

public class MyException : Exception
{
    public MyException()
    {

    }
}

The program breaks on b /= a; whereas I'm expecting it to go to the catch command. If I replace MyException with Exception, the exception is caught and the program doesn't break.

How can I catch a custom exception?

Was it helpful?

Solution 2

As mentioned in the comments the problem is not that you can't catch your exception, the problem is that the code isn't throwing that type of exception. It throws a System.DivideByZeroException. If you want to test your code to see it catch your exception then just replace b /= a; with throw new MyException(); and you will see it catch your exception. It catches something when you use the base class Exception because the DivicdeByZeroException also inherits from Exception.

Keep in mind the only way your exception will ever be thrown is if you have the line throw new MyException(); somewhere. You can make all the custom exceptions you want but .NET libraries aren't going to just start throwing them for you. In this case you shouldn't even be using a custom exception, if this is a learning exercise that's fine but it just doesn't really make sense when you already have an informative exception being thrown.

OTHER TIPS

Your code can't catch a MyException, because none is thrown; obviously you can't catch an exception that isn't thrown... This code, however, would throw and catch MyException:

static void Main(string[] args)
{
    try
    {
         Foo();
    }
    catch (MyException ex)
    {
         Console.WriteLine(ex.Message)
    }
}

static void Foo()
{
    throw new MyException()
}

As Random832 points out, the line b /= a throws a System.DivideByZeroException. A DivideByZeroException does not equal a MyException.

You can catch an exception of type Exception because a DivideByZeroException extends Exception. Your MyException also extends Exception, but it's a more derived type of Exception that is not a DivideByZeroException.

What exception handling does is essentially traverse up the hierarchy. There's no handler for DivByZero, maybe for its parent, then its parent and so on. The parent of all exceptions is Exception, so that's why your code catches eventually. Why it doesn't catch MyException is because there is no link from DivByZero to MyException. MyException is on a separate branch.

It is just the other way round: If you define

public class MyException : DivideByZeroException
{
    public MyException() { }
}

and use

static void Main(string[] args)
{
    try
    {
         throw new MyException();
    }
    catch (DivideByZeroException ex)
    {
         Console.WriteLine("Exception caught");
    }
}

then your catch would work. catch catches the exception you specify, and its descendants.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top