Introduction to ASP. NET Exception management

Source: Internet
Author: User

In any application system development, ASP. NET Exception management is a key field for developers. As a developer, you should adopt an appropriate exception management policy that allows you to write robust and high-quality code. If used properly, exception management will be a powerful concept and make your development work easier. However, an inappropriate Exception Handling will reduce the performance of your application system.

Before you study this, you must first understand what ASP. NET Exception management is. This is very important. In general, an exception indicates "Breaking the assumptions of the system Reservation ". "Exception" and "error" are different. For clarity, let's look at two examples:

Example 1

Let's assume that you are trying to write data to a file, and your application thinks that the file is in the correct path. If not, an exception is thrown. If your duty is to keep track of this file, the Code still cannot find the file.) exceptions will be a bad coding habit, in this case, the code should be verified for processing rather than exception ).

Example 2

Let us assume that in a general ASP. NET program, you are trying to update all the required fields in the database. Your application considers the database connection to be available. Suppose the connection is actually unavailable ...... Throwing an exception is a solution. Let's talk about it again. If there are several fields with null values when updating the required fields of the database, it is unnecessary to throw an exception. These processes should be completed by the Verification code.

How to handle exceptions

As a developer, you should feel the advantages of constructing a structured exception handling mechanism through try, catch, and finally blocks .. The. NET Framework provides a large number of exception handling levels to handle different types of exceptions. All exceptions are inherited from the Exception base class ). You can implement custom error handling through inheritance to extend the exception handling mechanism. Unfortunately, many developers misuse this architecture capability. One thing to remember at any time is how the Architecture Works when an exception occurs at runtime? Generally, there are three situations:

Ignore the exception and let it rise in the call stack and be caught by other catch blocks.

Capture exceptions and execute necessary actions for your application if you do not want to throw an exception again.

Capture exceptions and overwrite them with other exceptions. This is more closely related to your application. Exception coverage is used to avoid breaking the abstract hierarchy in the architecture. You can use the InnerException attribute of the exception you throw to specify what the original exception is, so that you can overwrite your existing exception with a new exception, which is more related to your system ). To understand exception overwriting, let's look at a method that can cause an IOException. You can use LoadingException or FailtoLoadInfoException at the application level to overwrite the original IOException, this is better than giving users the underlying IOException.

The exception handling framework of an application should have the following requirements ):

Detection exception:
◆ Clear the Execution Code;
◆ Internal exception coverage;
◆ Internal exception replacement;
◆ Record and report error information;
◆ Establish events that can be monitored externally to help system operations;

At the beginning, you should establish a consistent and robust ASP. NET Exception management architecture. In all your systems, you should well encapsulate and abstract details such as its records and reports.

  1. Data source controls in ASP. NET
  2. Introduction to the use of XML Web Services in ASP. NET
  3. Web. config file of ASP. NET application
  4. Overview ASP. net xml Web Services
  5. Implement HTTP requests in ASP. NET

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.