15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

15<br />

errors <strong>and</strong> exceptions<br />

WhaT ’ s in This ChaPTer<br />

➤<br />

➤<br />

➤<br />

Looking at the exception classes<br />

U s in g try...catch...finally to capture exceptions<br />

Creating user - defi ned exceptions<br />

Errors happen, <strong>and</strong> they are not always caused by the person who coded the application. Sometimes<br />

your application will generate an error because of an action that was initiated by the end user of your<br />

application or it might be simply due to the environmental context in which your code is running. In<br />

any case, you should anticipate errors occurring in your applications <strong>and</strong> code accordingly.<br />

The .<strong>NET</strong> Framework has enhanced the ways in which you deal with errors. <strong>C#</strong> ’ s mechanism for<br />

h<strong>and</strong>ling error conditions allows you to provide custom h<strong>and</strong>ling for each type of error condition as<br />

well as to separate the code that identifi es errors from the code that h<strong>and</strong>les them.<br />

By the end of this chapter, you will have a good grasp on advanced exception h<strong>and</strong>ling in your <strong>C#</strong><br />

applications.<br />

No matter how good your coding is, your programs should have the ability to h<strong>and</strong>le any possible<br />

errors that may occur. For example, in the middle of some complex processing of your code, you may<br />

discover that it doesn ’ t have permission to read a fi le, or, while it is sending network requests, the<br />

network may go down. In such exceptional situations, it is not enough for a method to simply return<br />

an appropriate error code — there might be 15 or 20 nested method calls, so what you really want<br />

the program to do is jump back up through all those 15 or 20 calls to exit the task completely <strong>and</strong><br />

take the appropriate counteractions. The <strong>C#</strong> language has very good facilities to h<strong>and</strong>le this kind of<br />

situation, through the mechanism known as exception h<strong>and</strong>ling.<br />

If you are a Java or C++ developer you are familiar with the principle of exceptions<br />

because these languages h<strong>and</strong>le errors in a similar way to <strong>C#</strong>. Developers using C++<br />

are sometimes wary of exceptions because of possible C++ performance implications,<br />

but this is not the case in <strong>C#</strong>. Using exceptions in <strong>C#</strong> code in general does not adversely<br />

affect performance. Visual Basic developers will fi nd that working with exceptions in <strong>C#</strong><br />

is very similar to using exceptions in Visual Basic (except for the syntax differences).<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!