Methods in C# - try Statements and Exceptions
(Page 4 of 4 )
A try statement specifies a code block subject to error-handling or cleanup code. The try block must be followed by a catchblock, afinallyblock, or both. Thecatchblock executes when an error occurs in thetryblock. Thefinallyblock executes after execution leaves thetryblock (or if present, thecatchblock), to perform cleanup code, whether or not an error occurred.
Acatch block has access to anExceptionobject that contains information about the error. You use acatch block to either compensate for the error or rethrow the exception. You rethrow an exception if you merely want to log the problem, or if you want to rethrow a new, higher-level exception type.
Afinallyblock adds determinism to your program, by always executing no matter what. It’s useful for cleanup tasks such as closing network connections.
Atrystatement looks like this:
try
{
... // exception may get thrown within execution of this block
}
catch (ExceptionA ex)
{
... // handle exception of type ExceptionA
}
catch (ExceptionB ex)
{
... // handle exception of type ExceptionB
}
finally
{
... // cleanup code
}
Consider the following program:
class Test
{
static int Calc (int x) {return 10 / x;}
static void Main()
{
int y = Calc (0);
Console.WriteLine (y);
}
}
Becausexis zero, the runtime throws aDivideByZeroException, and our program terminates. We can prevent this by catching the exception as follows:
class Test
{
static int Calc (int x) {return 10 / x;}
static void Main()
{
try
{
int y = Calc (0);
Console.WriteLine (y);
}
catch (DivideByZeroException ex)
{
Console.WriteLine("x cannot be zero");
}
Console.WriteLine ("program completed");
}
}
OUTPUT:
x cannot be zero
program completed
When an exception is thrown, the CLR performs a test:
Is execution currently within a trystatement that can catch the exception?
- If so, execution is passed to the compatiblecatchblock. If thecatchblock successfully finishes executing, execution moves to the next statement after thetrystatement (if present, executing thefinallyblock first).
- If not, execution jumps back to the caller of the function, and the test is repeated (after executing anyfinallyblocks that wrap the statement).
If no function takes responsibility for the exception, an error dialog is displayed to the user, and the program terminates.
Please check back next week for the continuation of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter four of C# 3.0 in a Nutshell, Third Edition, A Desktop Quick Reference, written by Joseph Albahari and Ben Albahari (O'Reilly; ISBN: 0596527578). Check it out today at your favorite bookstore. Buy this book now.
|
|