Reputation: 4762
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?
Upvotes: 0
Views: 4400
Reputation: 13315
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.
Upvotes: 0
Reputation: 2027
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.
Upvotes: 1
Reputation: 21887
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
.
Upvotes: 2
Reputation: 48096
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.
Upvotes: 2
Reputation: 292425
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()
}
Upvotes: 4