C # Exception Handling (1)

Source: Internet
Author: User

Mr. Jon jarger is a person with rich experiences. His articles are simple and thought-provoking. If you can browse on his website, you will certainly benefit a lot. Although this article is relatively simple, it will inevitably lead to errors in translation. I hope you will give me more advice.
PS: This article is very simple. If you feel that you have reached a certain level, please do not waste time ^_^

1. Painful stylized error handling

Before an exception occurs, the most classic way to handle the error is to use the error code to check the statement. For example
Public sealed class Painful
{
...
Private static char [] ReadSource (string filename)
{
FileInfo file = new FileInfo (filename );
If (errorCode = 2342) goto handler;
Int length = (int) file. Length;
Char [] source = new char [length];
If (errorCode =-734) goto handler;
TextReader reader = file. OpenText ();
If (errorCode = 2664) goto handler;
Reader. Read (source, 0, length );
If (errorCode =-5227) goto handler;
Reader. Close ();
Process (filename, source );
Return source;
Handler:
...
}
}
This encoding method is tedious and difficult to use. It looks complicated and makes the basic functions unclear. It is also easy to ignore errors (intentionally or occasionally forgotten ). Now, there are many ways to deal with this situation, but some of them must be better than others.

2. Relationship Separation

The most basic thing an exception can do is to allow you to separate errors from basic functions. In other words, we can rewrite the above as follows:
...
Public sealed class PainLess
{
Public static int Main (string [] args)
{
Try
{
String filename = args [0];
Char [] source = ReadSource (filename );
Process (filename, source );
Return 0;
}
Catch (SecurityException caught ){...}
Catch (IOException caught ){...}
Catch (OutOfMemoryException caught ){...}
...
}

Private static char [] ReadSource (string filename)
{
FileInfo file = new FileInfo (filename );
Int length = (int) file. Length;
Char [] source = new char [length];
TextReader reader = file. OpenText ();
Reader. Read (source, 0, length );
Reader. Close ();
Return source;
}
}
Pay attention to the following points during conversion:

1. I used numbers as error codes to describe errors. (For a very failed method, who knows what 2342 means ?), Use the named exception class to describe it (for example, SecurityException ).

2. The relationship between exception classes is not closely related to each other. On the contrary, the integer code used to describe a type of error must be unique throughout the error description code.

3. No detailed description is thrown in the ReadSource method. It is not necessary to throw a description in C.

However, it is worth noting that the ReadSource is very clear, simple, and clear when comparing two pieces of code. It only contains the statements that need to implement its basic functions, and does not show obvious error handling. This is acceptable, because if an exception occurs, the call stack will expand itself. This version is the desired "ideal" version.

However, exceptions allow us to get close to the ideal version of this ReadSource and prevent us from reaching it. ReadSource is an encoding example. It requests resources (a TextReader), uses resources (Read), and releases resources (Close ).

The problem is that if an exception occurs during resource request, the resource will not be released. The solution to this problem is part of this article. However, the "ideal" ReadSource version is still useful. We will use it as a reference for the following versions of ReadSource comments.

Related Article

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.