Skip to content

How can implement class which derived from system.exeption?

Avi Ozer

Asked Jan 28, 2006, 6:27 AM EST1 answer
Share
LinkedInX
I tried to implement that code:

try
{
  CheckDivideByZero(num1, num2);
}
catch (FirstDivideByZeroException)
{
  Console.WriteLine("first number is zero");
}

I need  that, FirstDivideByZeroException class will handle that Exeption instead of DevideByZeroExeption.
I tried to do that:

public  class
FirstDivideByZeroException : DivideByZeroException {}

but when i run the application, that message appear:
"An unhandled exception of type 'System.DivideByZeroException' occurred .."


How can i implement that?

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    u need to catch this exception with DivideByZeroException class and then again throw ur custom exception and catch the newly throwed exception outside the current scope . something like below:

    TypeScript
    try
    {
    try
    {
    DivideByZero(num1,num2);
    }
    catch(DivideByZeroException ex)
    {
    throw new CustomDivideByZeroException("Custom error :: " + ex.Message);
    
    }
    }
    catch(CustomDivideByZeroException exx)
    {
    MessageBox.Show(exx.Message);
    }
    
    *****
    
    public class CustomDivideByZeroException:System.Exception
    {
    public CustomDivideByZeroException():base()
    {}
    public CustomDivideByZeroException(string message):base(message)
    {}
    
    public CustomDivideByZeroException(string message , Exception ex):base(message,ex)
    {
    
    }
    }
    
    if anybody know better solution plz post it here.
    0