- Inherits from the System.ApplicationException class and uses exception as the end of the custom exception class name
- Three constructors: A parameterless constructor, a constructor for a string argument, a string argument, an inner exception as a parameter constructor
1 Public classmyexception:applicationexception2 {3 Private stringerror;4 PrivateException innerexception;5 //parameterless Constructors6 Publicmyexception ()7 {8 9 }Ten //a constructor with a string argument that passes the custom exception information when the programmer uses the exception class to get exception information rather than MyException One PublicMyException (stringMSG):Base(msg) A { - This. error=msg; - } the //constructor with a string argument and an inner exception information parameter - PublicMyException (stringMsg,exception innerexception):Base(msg) - { - This. innerexception=innerexception; + This. error=msg; - } + Public stringGetError () A { at returnerror; - } -}
Custom Exceptions
- Throw a custom exception
throw new MyException ("message");
Capture Custom exception: catch (MyException) {}
C # Custom Exceptions