Custom Exception Handling
-
Hello, I want a custom class "MyException" inherited from Exception class which will be used exactly same as the Exception class i.e. "MyException" must handle all the exceptions thrown by the try block. like public TestException { try { int i=0,j=0; j=25/i; } catch(MyException ex) { MessageBox.Show(ex.Message); } } The catch block must be respond the same way as if i use catch(Exception ex) which can handle all the exception. Thanks
-
Hello, I want a custom class "MyException" inherited from Exception class which will be used exactly same as the Exception class i.e. "MyException" must handle all the exceptions thrown by the try block. like public TestException { try { int i=0,j=0; j=25/i; } catch(MyException ex) { MessageBox.Show(ex.Message); } } The catch block must be respond the same way as if i use catch(Exception ex) which can handle all the exception. Thanks
-
Hello, I want a custom class "MyException" inherited from Exception class which will be used exactly same as the Exception class i.e. "MyException" must handle all the exceptions thrown by the try block. like public TestException { try { int i=0,j=0; j=25/i; } catch(MyException ex) { MessageBox.Show(ex.Message); } } The catch block must be respond the same way as if i use catch(Exception ex) which can handle all the exception. Thanks
public class MyException : Exception
{
}...
try
{
throw new MyException();
}
// uncomment the following code to catch the exception as a MyException
//catch (MyException e)
//{
// Console.WriteLine("MyException caught! {0}", e.ToString());
//}
catch (Exception e)
{
Console.WriteLine("Exception caught! {0}", e.ToString());
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hello, I want a custom class "MyException" inherited from Exception class which will be used exactly same as the Exception class i.e. "MyException" must handle all the exceptions thrown by the try block. like public TestException { try { int i=0,j=0; j=25/i; } catch(MyException ex) { MessageBox.Show(ex.Message); } } The catch block must be respond the same way as if i use catch(Exception ex) which can handle all the exception. Thanks
Nothing in your sample code throws a MyException object. Therefore you will never catch a MyException. It would appear to me that you don't understand how exceptions work. What benefit to you anticipate your custom exception having over catching an Exception? If I knew that then maybe I could understand what you are actually trying to do, and then I could guide you better.
Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog
-
Nothing in your sample code throws a MyException object. Therefore you will never catch a MyException. It would appear to me that you don't understand how exceptions work. What benefit to you anticipate your custom exception having over catching an Exception? If I knew that then maybe I could understand what you are actually trying to do, and then I could guide you better.
Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog
I think the contents of the try block was made up and had nothing to do with his problem. :) It seems that he does have a try-block that calls into something which might throw MyException, and the problem he had was that when *another* exception occured it was not caught. The correct pattern to use has already been provided; I'll just add that the rule is to catch the most specific exception first, then less specific ones. try { ... } catch (MyException mex) { ... // runs if try-block throws MyException } catch (Exception ex) { } Finally, I'll just mention that a finally block might come in handy in some of these cases, such as when "cleanup" of any kind is needed - eg. close a database connection.
-
I think the contents of the try block was made up and had nothing to do with his problem. :) It seems that he does have a try-block that calls into something which might throw MyException, and the problem he had was that when *another* exception occured it was not caught. The correct pattern to use has already been provided; I'll just add that the rule is to catch the most specific exception first, then less specific ones. try { ... } catch (MyException mex) { ... // runs if try-block throws MyException } catch (Exception ex) { } Finally, I'll just mention that a finally block might come in handy in some of these cases, such as when "cleanup" of any kind is needed - eg. close a database connection.
That is probably what he was actually looking for, but he did say '"MyException" must handle all the exceptions thrown by the try block'
Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog