(a) Introduction
The appearance of errors is not always the cause of the person writing the application, and sometimes the application can get an error because of an environment in which the application's end user throws or runs the code. C # provides an exception handling mechanism to handle errors.
(b) Exception class
In C #, an exception object is created (or thrown) when a special exception error condition has just occurred. In general, exceptions do not have a specific namespace, and the exception class should be placed in the namespace of the class where the exception was generated.
(iii) Catching exceptions
The. NET Framework provides a large number of pre-defined base class exception objects. In order to handle possible error situations in C # code, it is common to divide the relevant parts of the program into 3 different types of code blocks.
The try block contains code that makes up the normal operation of the program, but this part of the program may experience some serious errors.
The catch block contains code that handles various error conditions that are encountered when executing code in a try block.
The finally block contains code that cleans up resources or performs other operations that are typically performed at the end of a try block or catch block, regardless of whether an exception is thrown or not, and the finally block executes.
Try { // code }catch (Exception) { // Multiple catch snaps can be set to catch different exceptions (snapping order from top to bottom)}finally { // can be omitted }
1. Implement multiple Catch blocks
try { / / code } Catch (IOException) { // Multiple catch snaps can be set to catch different exceptions (snapping order from top to bottom) } catch (Exception) { // Multiple catch snaps can be set to catch different exceptions (snapping order from top to bottom) } finally { // You can omit }
the first written catch blocks are used to handle very special error conditions, followed by more general blocks, which can handle any error, and we do not write specific error handlers for them. The order of catch blocks is important, and if you write these two blocks in reverse order, the code will not compile because the second catch block will not be executed.
2. System.Exception Properties
varException =NewException (); Exception. data["errordate"] = DateTime.Now;//the first way to add extra information for an exceptionexception. Data.add ("AdditionalInfo","exception Additional Information");//the second way to add extra information for exceptionsexception. HelpLink ="Excption.txt";//exception's Help fileexception. Source ="Test Exception Application";//the application name or object name that caused the exception
above is the writable properties of the exception, in addition to the readable properties. InnerException is thrown in a catch block, it contains the exception object that sends the code to the catch block, the text of the message describing the error condition, and the details of the method call on the StackTrace stack. TargetSite. NET reflection object that describes the method that throws the exception.
3. Nested Try Blocks
1 Try2 {3 //Outer code a4 //(Throws an exception here, handled by the outer exception)5 Try6 {7 //inner-layer code8 //(Throws an exception here, if the inner exception can handle it, otherwise handled by the outer layer exception)9 //Note: If the inner layer cannot handle the exception, after the finally method is executed,Ten //Look for outer match exception handling, this time the outer code B is not running One } A Catch(Exception)//intra-layer exception handling - { - //you can set multiple catch captures for different exceptions (snapping order from top to bottom) the //(Throws an exception here, handled by the outer exception) - } - finally - { + //can be omitted - //(Throws an exception here, handled by the outer exception) + } A //Outer code B at //(Throws an exception here, handled by the outer exception) - } - Catch(Exception)//Outer exception Handling - { - //you can set multiple catch captures for different exceptions (snapping order from top to bottom) - } in finally - { to //can be omitted +}
(iv) User-defined exception classes
class program{ static void Main (string Span style= "COLOR: #000000" >[] args) { try { // 2, throws a custom exception throw new MyException ( " custom exception information " catch (MyException ex) // {Console.WriteLine (ex. Message); } }}
// 1. Custom exception Classes Public class myexception:exception{ // Passing exception information to the base class constructor public myexception ( String message):base(Message) {} public myexception (string base(Message,innerexception) {}}
(v) Caller information
Gets information about where the error occurred when processing the error. C#5.0 provides a new feature that can be used to obtain this information (defined in System.Runtime.CompilerServices) with attributes and optional parameters.
1 Static voidMain (string[] args)2 {3 varp =NewProgram ();4 P.Log ();5P.someproperty = -;6Action a = () =P.Log ();7 a ();8 }9 Private intSomeproperty;Ten Public intSomeproperty One { A Get{returnSomeproperty;} - Set { - This. Log (); theSomeproperty =value; - } - } - Public voidLog ([Callerlinenumber]intline =-1, [Callerfilepath]stringPath =NULL, [Callermembername] + stringName =NULL) - { +Console.WriteLine (Line <0) ?"No Line":" Line"+Line ); AConsole.WriteLine (Path = =NULL) ?"No file path":" Line"+path); atConsole.WriteLine ((Name = =NULL) ?"No member name":" Line"+name); - Console.WriteLine (); -}
Run the above code with the following results:
"Reading notes" C # Advanced Programming chapter 16th errors and exceptions