- First, the exception handling of C # uses the keyword
- Try is used to check for exceptions that occur and to help send any possible exceptions.
- Catch handles errors in a much more controlled manner, and can have multiple catch clauses.
- Finally whether or not an exception is thrown,finally the code block will be executed.
- Throw is used to throw exceptions, which can throw predefined and custom exceptions.
- Ii. format of C # exception handling
- Try
- {
- program code block;
- }
- catch (Exception e)
- {
- Exception handling code block;
- }
- Finally
- {
- code block to be executed, whether or not an exception occurs;
- }
- Third, exception handling combat
- A simple example of divisor and zero:
- Public class Divisoriszero
- {
- Private static void Main ()
- {
- int dividend=10;
- int divisor1=0;
- int divisor2=5;
- int dividevalue;
- Try
- {
- Dividevalue=dividend/divisor1; //(1)
- Dividevalue=dividend/divisor2; (2)
- System.Console.WriteLine ("dividevalue={0}", Dividevalue); (3) This line will not be executed.
- }
- Catch
- {
- System.Console.WriteLine ("passed the exception value is: {0}", e);
- }
- Finally
- {
- System.Console.WriteLine ("I will show whether or not an exception occurred.) ");
- }
- }
- }
- Note: (1) An exception is thrown if the row is executed, and if there is no catch statement, the program terminates abnormally, using a catch clause without parameters, you can catch any type of exception.
- If the (1) line is commented out, the (2) line is enabled, which means that the program runs without an exception, and from the output, thefinally code block will still be executed.
- You can provide multiple catch statements to a try statement to catch a specific exception, as in the previous example: 0 as a divisor throws an exception of type DivideByZeroException, and the catch statement in the previous example can be modified as follows:
- catch (DivideByZeroException e)
- {
- System.Console.WriteLine ("0 cannot be a divisor!") The exception value is: \n{0} ", E);
- }
- catch (Exception e)
- {
- System.Console.WriteLine ("not \ ' 0 exception thrown by divisor \"! Exception value: \n{0} ", E);
- }
- Why add a catch (Exception e) clause? The reason is simple, thecatch (dividebyzeroexception e) clause catches only a specific exception, andthe program code inside the try may also produce other exceptions that can only be caught by catch (Exception e).
- The following table shows some common exceptions:
- Common exception classes in the System namespace
- Exception class name Simple description
- Memberaccessexception Access Error: Type member cannot be accessed
- ArgumentException parameter error: Invalid parameter for method
- The ArgumentNullException parameter is empty: an unacceptable null argument is passed to the method
- ArithmeticException Mathematical calculation error: Due to the mathematical operation caused by the anomaly, wide coverage.
- ArrayTypeMismatchException Array Type mismatch
- DivideByZeroException was 0 apart
- The FormatException parameter is not properly formatted
- IndexOutOfRangeException Index is out of range, less than 0 or larger than the last element's index
- InvalidCastException illegal coercion, thrown when an explicit conversion fails
- MulticastNotSupportedException Unsupported Multicast: Thrown when combining two non-null delegates failed
- The method called by NotSupportedException is not implemented in the class
- NullReferenceException when referencing a null reference object
- OutOfMemoryException thrown when memory cannot be allocated for new statements, not enough memory
- OverflowException Overflow
- StackOverflowException Stack Overflow
- TypeInitializationException the initialization type of the error: thrown when a static constructor has a problem
- NotFiniteNumberException Infinite Value: The number is not legal
- Iv. defining your own exception classes
- In addition to the predefined exceptions, we can create our own exceptions, and the process is relatively straightforward:
- One declares an exception, in the following format:
- Class exceptionname:exception{}
- Two throws their own exception:
- throw (Exceptionname);
- See an example:
- Class iamsecondgrade:system.exception{}//declaring exceptions
- Class Secondgrade
- {
- Public static int mul (int First,int second)
- {
- if (first>100| | SECOND>100)
- Throw new Iamsecondgrade (); Throw Exception
- return (First*second);
- }
- Public static void Main ()
- {
- int mul_value;
- Try
- {
- Mul_value=mul (99,56);
- System.Console.WriteLine ("99 and 56 product: {0}", Mul_value);
- Mul_value=mul (101,4);
- System.Console.WriteLine ("There is an exception, this line will not be executed.") ");
- }
- catch (Iamsecondgrade)//Capture Custom exceptions
- {
- System.Console.WriteLine ("I'm only in second grade, over 100 multiplication I won't. Hey, I'm customizing the exception. ");
- }
- catch (System.Exception e)
- {
- System.Console.WriteLine ("Non-custom exception. The value is: {0} ", e);
- }
- }
- }
C # Basic point of Try catch exception trapping mechanism