ADBRITE ads links
You are here: CodeIdol.com > C# > Learning C# 2005 > Throwing And Catching Exceptions
Learning C# 2005
| 16.1. Bugs, Errors, and Exceptions
It is important to distinguish exceptions
from bugs
and errors
. A bug is a programmer mistake that should be fixed before the code is shipped. An exception is not the result of a programmer mistake (t...
|
|
| 16.2. Throwing Exceptions
All exceptions are either of type System.Exception
or of types derived from System.Exception. Microsoft suggests that all the exceptions you use in your program derive from System.Exception, though you a...
|
|
| 16.3. Searching for an Exception Handler
When your program encounters an exceptional circumstance, such as running out of memory, it throws an exception. Exceptions must be handled before the program can continue.
If the currently running functio...
|
|
| 16.4. The throw Statement
To signal an abnormal condition in a C# program, throw an exception by using the tHRow keyword. The following line of code creates a new instance of System.Exception and then throws it:
throw n...
|
|
| 16.5. The try and catch Statements
As you saw, the exception in your previous example stopped your program dead. That's usually not the desired behavior. What you need is a way to tell the compiler, "If any exceptions are t...
|
|
| 16.6. How the Call Stack Works
Examine the output of Example 16-2 carefully. You see the code enter Main( ), Func1( ), Func2( ), and the TRy block. You never see it exit the try block, though it does exit Func2( ), Func1( ), an...
|
|
| 16.7. Creating Dedicated catch Statements
So far, you've been working with generic catch statements only. You can create dedicated
catch statements that handle only some exceptions and not others, based on the type of exception thro...
|
|
| 16.8. The finally Statement
In some instances, throwing an exception and unwinding the stack can create a problem. For example, if you opened a file or otherwise committed a resource, you might need an opportunity to close ...
|
|
| 16.9. Exception Class Methods and Properties
So far you've been using the exception as a sentinelthat is, the presence of the exception signals the errorsbut you haven't touched or examined the Exception object itself. The Sys...
|
|
| 16.10. Custom Exceptions
The intrinsic exception types the CLR provides, coupled with the custom messages shown in the previous example, will often be all you need to provide extensive information to a catch block when an exception is thrown...
|
|
| 16.11. Summary
Throwing (or raising) an exception halts execution of your program in place, and execution proceeds in the most immediately available catch block (exception handler). If the exception was not raised within a TRy block, or there is n...
|
|
| 16.12. Quiz
Question 161.
What is an exception?
Question 162.
What does the framework do if no exception handler is found in the method that throws an event?
Question 163.
How do you create a handler?
Question 164.
What is the synta...
|
|
You are here: CodeIdol.com > C# > Learning C# 2005 > Throwing And Catching Exceptions
|
|
Related tags
Popular Categories
Unix books and guides
AJAX popular information
C# language guides
Windows books and cookbooks
.......
|
|