Runtime exception and checked exception

Source: Internet
Author: User
Tags finally block

Basic knowledge

During program development, data streams are transmitted according to certain rules. If the interaction between the user and the program runs completely according to the predefined effect, the program will not be faulty. It can be assumed that the processed data meets the requirements and the format is determined through the interface. All resources are correct. However, for the sake of system security, you need to deal with the existing risks, do not idealize data security.

In ASP. NET, error processing is divided into two layers: Page input information verification and the structured error processing mechanism provided by CLR.

You can use JavaScript, VBScript, and other scripts to determine the input information on the page. the validation and Regular Expression Methods in the. NET environment determine the format before submitting and processing the information. Format determination includes determining the network address, email address, length of input string, and data type of input information.

Structured exception handling is a basic part of Clr and has the following features: it can be captured and processed in another language and thrown in another language, an exception can include another exception, which means that the system can capture exceptions of underlying objects (such as the data layer and business layer) and cause its own exceptions, including exceptions of underlying objects. In this way, exceptions can be classified in detail, making it easier to handle exceptions.

Structured exception handling usually requires three parts of code.
 
(1) Try: The operation part in the program that may cause errors.

(2) catch: the part used to handle various errors (there may be multiple ). The catch clause to capture exceptions must be correctly arranged, and the catch clause with a small range of exceptions should be placed in the front. That is, if there is an inheritance relationship between exceptions, the exception of the subclass should be placed in the catch clause before.

(3) The Finally block code is used to clear resources or execute other operations to be performed at the end of the try block (which can be omitted ). Whether or not an exception occurs, finally blocks are executed.

Structured Exception Handling

Exceptions are unavoidable errors in programming. The designed program must be able to handle all possible errors. Therefore, we need to fully consider Exception Handling, capture all codes that may encounter exceptions by try, and establish our own Exception Handling Mechanism to classify and process exceptions according to different situations.

When an exception occurs, you need to know the cause of the error and related information. Throws a specific type of exception based on the actual situation, and establishes a capturing mechanism to handle the exception. During code writing, you can instantiate and throw the required exceptions using the related and custom exception classes defined by the system. If an interface cannot be implemented, the system. notsupportedexceptiion exception can be thrown to notify the caller of the interface.

When handling exceptions, you should handle the specific exceptions that can be handled in the Catch Block. Otherwise, the program will stop running. Handle each exception in different ways to avoid handling all exceptions in the same way. In addition, when an exception occurs, give the user a friendly prompt (the end user does not understand the specific content of the system exception, which requires related information prompts and solutions, or tell the Administrator), and provide the user with a selection (OK or cancel) If possible, so that the user can decide the operation direction of the system. At the same time, exceptions must be logged in the program. Not all exceptions must be recorded. For example, you do not need to record exceptions that can be captured and are scheduled to be handled by programs.

You can record exceptions as follows:

An exception is recorded in the file. It is easy for technicians to view the exceptions and improve the program in the future.

An exception is recorded in the database. The Database supports query, so that you can perform classification query and other operations on exceptions in the future for ease of viewing and management.

An exception is recorded in EventLog. The system administrator can remotely monitor all computer exceptions.

In addition to predictable exceptions, there are also unexpected exceptions. Such exceptions cannot be managed. After a system program is transferred to a specific error page, the system prompts the user to go wrong and end the program. Logs can be used to solve and debug problems. Avoid using try-catch but not handling exceptions. Otherwise, it is equivalent to releasing exceptions (in this case, it is better not to capture them at all ).

For Exception Handling, you should also pay attention to releasing related resources in the Finally block and restoring relevant settings. The basic program structure is as follows:

Try

{

Int x = 123/0; // an exception with a division of 0 is thrown.

}

Catch

{

//... // Capture the exception here and handle it accordingly

}

Finally

{

//... // Do the final cleaning or other work here

}

Exception Handling in this Chapter applies this technology to areas that are prone to errors (operating databases). However, after capturing SQL exceptions, you will be given an error message, no further program processing is performed.

C # Common exceptions

Exception: The base class of all Exception objects.

Systemexception: The base class of all errors generated during the runtime.

Indexoutofrangeexception: It is triggered when the subscript of an array exceeds the range.

Nullreferenceexception: triggered when an empty object is referenced.

Invalidoperationexception: when a call to a method is invalid for the current state of the object, it is triggered by some methods.

Argumentexception: The base class for all parameter exceptions.

Argumentnullexception: triggered by a method when the parameter is null (not allowed.

Argumentoutofrangeexception: When a parameter is not within a given range, it is triggered by a method.

Interopexception: The base class of an exception that occurs in or outside the CLR environment.

Comexception: an exception that contains the hresult information of the COM class.

Sehexception: an exception that encapsulates Win32 structure exception handling information.

Sqlexception: encapsulates SQL operation exceptions.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.