Introduction: Jon Jagger President is a person through a rich, simple and thought-provoking articles written. We can visit his Web site to some, certainly benefited. This article is a relatively simple translation error which is inevitable, I hope we exhibitions.
PS: This article is very simple, if you feel you have reached a certain level, then, do not waste time ^ _ ^
1, programmable error handling painful
Exception not appear before the most classic way to handle errors is to use the error code checks the statement. Such as
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 tedious Fanlaifuqu, difficult to use, looks very complex, but also makes very clear the basic functions. And very easy to ignore the error (intentional or the occasional forgotten). Well now, there are many to deal with this situation, but there must be a number of approaches which are better than others.
Second, relationship separation
Exception can do the most basic thing is to allow you the basic functions of the error and to separate. 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;
)
)
In the conversion process, the need to note the following:
1.'ve Used numbers as error codes to describe the error (a doomed approach, who knows what 2342 mean?), Now used to describe the name of the exception classes (for example: SecurityException).
2. The relationship between exception class is not closely linked. On the contrary, used to describe a class of error of integer code describing the error code must be unique.
3. ReadSource method did not throw no details. Throw in the C # note is not required.
However, the most noteworthy are: To compare the two codes, ReadSource very clear, simple and clear. It now contains only the basic functions necessary to achieve its statement, did not show obvious error handling. This is because if the exception occurs, the call stack will be self-started. This version is we want to "ideal" version.
However, the exception allows us to close this ReadSource ideal version, the same time, we get it to stop. ReadSource is a code example, it requested resource (a TextReader), use of resources (Read), and the release of resources (Close).
The problem is if the requested resource exception occurred during, then resources will not be released. Solution of this problem is a part of this article. However, the "ideal" in the ReadSource version is still useful. We will use it as the following evaluation of several versions of ReadSource reference.
Recommended links:
Joan Chen: "Nuclear High-based" special to be drying faster
Jack Ma, Alibaba will open a Large envelope puzzle
Feature Library To Create Instance Of IDS Intrusion Analysis (2)
Wizard Icons
Modern Shipbuilding Process
Low back not just a Cheap PC
Infomation Gallery And Cataloging Tools
Convert m4a to mp3 online
how to convert avi to mpg
Fireworks produced blue and white porcelain button
Easy to use Terminal AND Telnet Clients
H264 format
10 tips to help you PASS the interview off
flv to mp3 converter free
Compare Games Sports
FMC from Theory to practice