Exception class
In C #, exception handling is a mechanism that C # provides for handling error conditions. It provides custom handling for each error condition and separates code that flags the wrong code from preprocessing the error.
For. NET classes, the generic exception class System.Exception is derived from System.Object. There are also many well-defined exception classes (for example: System.systemexpection, System.ApplicationException, etc.), they are derived from the System.Exception class. Where the System.ApplicationException class is a third-party defined exception class, if we want to customize the exception class, Then it should be derived from it.
To handle exceptions in code, you typically use three blocks of code:
The first code block: The code of the try block is the part of the program where errors can occur.
The second block of code is the part of the catch Block's code that handles various errors (there can be more than one). The catch clause that catches the exception must be correctly arranged, with a small range of exception placed in the preceding catch. That is, if there is an inheritance relationship between exception, The exception of the class should be placed in the preceding catch clause
The third block of code: The finally fast code is used to dedicate resources or perform other operations to be performed at the end of the try block (which can be omitted). and whether or not an exception occurs, the finally block executes.
Exception handling
Regardless of how well the program is written, exceptions can occur, and the program must be able to handle exceptions. So we have to stand in an unusual way to write an exception handler, to establish a good exception handling strategy for errors that may occur in the program.
When an exception arises, we want to know what is causing the error or exception. We can throw specific types of exceptions according to the actual situation, and make specific handling when catching the exception. In the process of writing code, you can use the related exception class that the system has already defined and the custom exception class, = To instantiate and throw the exception we need. As an interface that is not possible, we can throw "system.notsupportedexception" exceptions to tell the caller of the interface.
When dealing with exceptions, we should handle the specific exceptions that can be handled in the catch block, or the program will terminate. Treat each exception in a different way, avoiding the same treatment for all exceptions. It's like a doctor can't prescribe the same medicine for a pregnant woman and a cold. When we were opening the pills, is not to be told how much to eat, bogey cold what attention matters, the same truth, in the abnormal production time, need to give the user a good hint (ordinary users of the specific content of the exception is not clear, which requires us to give relevant information and solutions, or contact, for example, we are pregnant, What is the principle of pregnancy? What is the cause of pregnancy? What is the effect of pregnancy? We generally may not know, we can only accept the occurrence of the situation of vomiting, what to do, to find a doctor, we just know that it is not normal, this is abnormal, and, where possible, to provide users with possible choices (termination, retry, ignore) , let the user decide how to run the program from. At the same time, the exception should be logged (cases). But not all exceptions must be documented, such as some predictable and error-solving errors that we do not need to log.
Record exceptions we can take the following methods:
1. Record exceptions in the file to make it easier for technicians to see what's going on and improve the program.
2. Log exceptions in the database. Database support query, so that in the late can be the exception of the classification of queries and other operations, easy to view and management
3. Record in EventLog: Remote operation allows the system administrator to monitor all computed anomalies.
In addition to specific, predictable anomalies, (what do I say next?), and unexpected errors, which we don't usually want to see, but will certainly happen, just as doctors don't want to see a case of cancer (because it means that the person is not going to be able to do it, and he usually wants us to live well and not to have any major illnesses.) , we still have to believe that there are good doctors in the world. But the exceptions that are not willing to see can only temporarily end the operation of the program, where the log can be done for us to solve and debug problems brought convenience. Also, to avoid using Try-catch but not handling exceptions, Otherwise it is equivalent to the abnormal release (you are sick, the doctor told you Gerze, you are dead.) That's not going to work.
After processing the exception, we also have to pay attention to release resources in the finally block, restore related settings information, such as finishing work, that is, after your illness, you need to discharge procedures, and nurse sister to sue individual.
When doing exception handling, it is best to use try-catch at all entrances to the application (event handlers, main functions, thread portals). But do not add try-catch at the entrance of the program constructor, because there is an exception here, and he is not able to handle it, because he is not yet constructed. You can only throw an exception on the outer layer.
In general, the use of exception handling mechanism to handle errors, can be the entire program structure clear, Code simple (flag error code preprocessing error code separation), but we also can not blindly use exceptions. And using exceptions can have some impact on the performance of the program (using exceptions in C # generally does not affect performance). For some simple, early-to-avoid errors, we should be dealing with it early outside of the try block.
Understand the basics, and then look at the C # exception class related summary
What you need to know is that all of the exception classes in C #, whether customized or system-defined, have their base classes exception
Also talk about C # common exception classes (no need to remember): know that there is such a thing, you can not look, do not remember, but I cannot but write.
Exception class name |
Description |
SystemException |
This class is the base class for all other exception classes in the System namespace. (Recommended: Exceptions thrown by the common language runtime typically use this class) |
ApplicationException |
This class represents the exception that is thrown when an application has a non-fatal error (Recommendation: Exceptions thrown by the application itself are typically used in this class) |
Exception class associated with a parameter |
ArgumentException |
This class is used to handle exceptions with invalid arguments, and in addition to inherited property names, this class also provides a string-type property paramname that represents the name of the parameter that threw the exception. |
FormatException |
This class is used to handle exceptions with malformed parameters |
Exceptions related to member access |
Memberaccessexception |
The class is used to handle the exception that is thrown when a member of the Access class fails. The reason for the failure may be that there is insufficient access, or that the member to be accessed does not exist at all (calls between classes are often used) |
Memberaccessexception |
See note below |
Array-related exception classes |
Indexoutofexception |
This class is used to handle exceptions that are thrown when the subscript exceeds the length of the array |
ArrayTypeMismatchException |
The class is used to handle exceptions thrown by elements that store incorrect data types in an array |
Rankexception |
The class is used to handle exceptions that are caused by dimension errors |
IO-related exception classes |
IOException |
This class is used to handle exceptions that are thrown when file input and output operations are performed. See the notes below for details |
Arithmetic-related exception classes |
ArithmeticException |
This class is used to handle arithmetic-related exceptions, as detailed in the remarks below |
Note:
Direct derived classes of the Memberaccessexception class:
I, Fileaccessexception class: This class is used to handle exceptions thrown by accessing field member failures
II, Methodaccessexception class: This class is used to handle exceptions thrown by access method member failures
III, MissingMemberException class: This class is used to handle exceptions that are thrown when a member does not exist
5 Direct derived classes of the IOException class:
I, Directionnotfoundexception class: This class is used to handle exceptions that are thrown when a specified directory is not found.
II, FileNotFoundException class: This class is used to handle exceptions that are thrown when a file is not found.
III, EndOfStreamException class: This class is used to handle the exception that is thrown when the end of the stream has been reached and continues to read the data.
IV, FileLoadException class: This class is used to handle exceptions that are thrown when a file cannot be loaded.
V, PathTooLongException class: This class is used to handle exceptions that are thrown because the file name is too long.
Derived classes of the ArithmeticException class:
I, DivideByZeroException class: Represents an exception that is thrown when an integer in the decimal operation attempts to divide by 0.
II, NotFiniteNumberException class: Represents the exception that is thrown when an infinite or non-negative value occurs in a floating-point operation.
C # programming (80)----------Exception class