Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Custom Exception Handling

Custom Exception Handling

Scheduled Pinned Locked Moved .NET (Core and Framework)
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shecool
    wrote on last edited by
    #1

    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

    B M C 3 Replies Last reply
    0
    • S shecool

      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

      B Offline
      B Offline
      Brij
      wrote on last edited by
      #2

      Please go throgh this link http://www.c-sharpcorner.com/uploadfile/susanabraham/customexceptionhandling06022005011154am/customexceptionhandling.aspx[^] Hope this will solve your problem :)

      Cheers!! Brij

      1 Reply Last reply
      0
      • S shecool

        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

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        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:

        1 Reply Last reply
        0
        • S shecool

          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

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          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

          D 1 Reply Last reply
          0
          • C Colin Angus Mackay

            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

            D Offline
            D Offline
            dojohansen
            wrote on last edited by
            #5

            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.

            C 1 Reply Last reply
            0
            • D dojohansen

              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.

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups