0

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?

yazanpro
  • 4,512
  • 6
  • 44
  • 66
  • 5
    Why would `b /= a` throw your exception? It throws a `System.DivideByZeroException`. If you want to catch your exception you have to throw your exception. What do you think you're accomplishing by creating a custom exception? – Random832 Sep 24 '13 at 16:07
  • because MyException is inherited from Exception. that's why I'm expecting to catch any exception. – yazanpro Sep 24 '13 at 16:08
  • If DevisionByZeroException is what thrown, since this exception is not castable to MyException it is not caught by catch(MyException) – Alireza Sep 24 '13 at 16:09
  • @yazanpro: You will catch instances of `MyException` and derived exception types. `DivideByZeroException` doesn't derive from `MyException` so you don't catch it. – Daniel Hilgarth Sep 24 '13 at 16:10
  • 4
    That's... not how it works. The exception isn't what does the catching. You've severely misunderstood both how exceptions work and how class inheritance works. – Random832 Sep 24 '13 at 16:11
  • 1
    **How can I catch a custom exception?** You must throw that custom exception or a subclass of that exception. – Bob Kaufman Sep 24 '13 at 16:11

5 Answers5

4

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()
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
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.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
2

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.

Dave Zych
  • 21,581
  • 7
  • 51
  • 66
1

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.

Yimin Rong
  • 1,890
  • 4
  • 31
  • 48
0

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.

FrankPl
  • 13,205
  • 2
  • 14
  • 40