First, the understanding of abnormal handling?
Exception handling refers to the process in which an error occurs that causes the program to exit, which is called an exception.
Therefore, handling this error is called exception handling.
Second, how to operate the exception handling?
C # exception handling is built on four keywords: try, catch, finally, and throw.
1. Try: A try block identifies a block of code for the specific exception that will be activated. followed by one or more catch blocks.
2. Catch: The program catches exceptions through an exception handler. The Catch keyword represents the catch of the exception.
3. The finally:finally block is used to execute the given statement, regardless of whether the exception is thrown.
For example, if you open a file, the file will be closed regardless of whether the exception occurred.
4, Throw: When the problem occurs, the program throws an exception. Use the Throw keyword to complete.
Syntax Examples:
Try{ //the statement that caused the exception}Catch(Exceptionname E1) {//Error Handling code}Catch(exceptionname E2) {//Error Handling code}Catch(Exceptionname EN) {//Error Handling code}finally{ //the statement to execute}
Third, exception classes in C #
C # Exceptions are represented by using classes. Exception classes in C # are primarily derived directly or indirectly from System.Exception
(1). The exception type derived by System.SystemException:
System.accessviolationexception The exception that is thrown when attempting to read and write protected memory.
System.ArgumentException The exception that is thrown when one of the arguments supplied to the method is invalid.
System.Collections.Generic.KeyNotFoundException Specifies the exception that is thrown when the key used to access an element in the collection does not match any key in the collection.
System.IndexOutOfRangeException when accessing an array, an exception that is thrown because the element index exceeds the bounds of the array.
System.InvalidCastException The exception that is thrown because of an invalid type conversion or display conversion.
System.InvalidOperationException The exception that is thrown when a method call is not valid for the current state of an object.
System.invalidprogramexception The exception that is thrown when a program contains invalid Microsoft intermediate language (MSIL) or metadata, which usually indicates that there is a bug in the compiler for the build program.
System.IO.IOException The exception that is thrown when an I/O error occurs.
System.NotImplementedException The exception that is thrown when a requested method or operation cannot be implemented.
System.NullReferenceException The exception that is thrown when an attempt is made to manipulate an empty object reference.
System.OutOfMemoryException The exception that is thrown when there is not enough memory to continue executing the program.
System.StackOverflowException The exception that is thrown when an execution stack overflow is caused by too many pending method calls.
(2). The exception type derived by System.ArgumentException:
System.ArgumentNullException The exception that is thrown when a null reference is passed to a method that does not accept it as a valid argument.
System.ArgumentOutOfRangeException The exception that is thrown when the parameter value exceeds the allowable range of values defined by the called method.
(3). The exception type derived by System.ArithmeticException:
System.DivideByZeroException The exception that is thrown when attempting to divide an integer value or decimal value with 0.
System.notfinitenumberexception The exception that is thrown when the floating-point value is positive infinity, negative infinity, or non-numeric (NaN).
System.OverflowException the exception that is thrown when an arithmetic operation, type conversion, or conversion operation in the selected context causes an overflow.
(4). The exception type derived by System.ioexception:
System.IO.DirectoryNotFoundException The exception that is thrown when a file or part of a directory is not found.
System.IO.DriveNotFoundException The exception that is thrown when an attempt to access a drive or share is not available.
System.IO.EndOfStreamException The exception that is thrown when the read operation attempts to exceed the end of the stream.
System.IO.FileLoadException The exception that is thrown when a managed program is found but cannot load it.
System.IO.FileNotFoundException an exception that is thrown when an attempt is made to access a file that does not exist on disk.
System.IO.PathTooLongException The exception that is thrown when the path name or file name exceeds the system-defined maximum length.
(5). Other common exception types:
ArrayTypeMismatchException attempted to store an object of the wrong type in the array.
BadImageFormatException graphics format is incorrect.
DivideByZeroException except 0 exceptions.
Dllnotfoundexception The referenced DLL was not found.
FormatException parameter format is incorrect.
Methodaccessexception attempts to access a private or protected method.
MissingMemberException accesses an invalid version of the DLL.
the method called by NotSupportedException is not implemented in the class.
This error is thrown when the PlatformNotSupportedException platform does not support a particular property.
Iv. examples
classmyexceptiontest{Static DoubleSafedivision (DoubleXDoubley) {if(Y = =0) Throw NewSystem.DivideByZeroException (); returnX/y; } Static voidMain () {DoubleA =98, B =0; Doubleresult =0; Try{result=Safedivision (A, b); Console.WriteLine ("{0} divided by {1} = {2}", A, b, result); } Catch(DivideByZeroException e) {Console.WriteLine ("attempted divide by zero."); } }}
Public number: Read the original
Summary of "Basic" C # exception handling