Exception Handling: Visual Programming Languages
Exception Handling: Visual Programming Languages
Exception Handling: Visual Programming Languages
1
Chapter Objectives
• Learn about exceptions, including how they are thrown and caught
• Gain an understanding of the different types of errors that are found
in programs
• Look at debugging methods available in Visual Studio
• Discover how the Debugger can be used to find run-time errors
2
Chapter Objectives (continued)
3
Errors
Quick
info
4
Run-Time Errors
5
Debugging in C#
• Desk check
• Many IDEs have Debuggers
• Debuggers let you observer the run-time behavior
• You can break or halt execution
• You can step through the application
• You can evaluate variables
• You can set breakpoints
• Debug menu offers debugging options
6
Debugging in C# (continued)
Select Start
Debugging
and number
of options to
run your
program
doubles
9
Breakpoints (continued)
• Continue
• Takes the program out of break mode and restores it to a run-time mode
• If more than one breakpoint set, Continue causes the program to execute
from the halted line until it reaches the next breakpoint
• Stepping through code
• Execute code line by line and see the execution path
• Examine variable and expression values as they change
13
Stepping Through Code
14
Watches
15
Watches (continued)
16
Exceptions
17
Exceptions (continued)
• Dialog box asks you whether you want to have an error report sent
to Microsoft
Normally
you do not
want to try
to debug
application
while it is
running Click
No
Figure 10 Just-In-Time Debugger
19
Unhandled Exception
20
Unhandled Exception (continued)
22
Bugs, Errors, and Exceptions
23
Bugs, Errors, and Exceptions (continued)
Stack
trace
25
Try…Catch…Finally Blocks
26
try
{
optional entry
// Statements
}
catch (ExceptionClassName exceptionIdentifier)
{
// Exception handler statements
} One catch
: // [additional catch clauses] clause
required
finally
{ finally clause
// Statements optional
}
27
Try…Catch…Finally Blocks (continued)
28
Use of Generic Catch Clause
Example
uses a generic catch block
Never quite
sure what
causes the
exception to
be thrown
when a
generic
catch clause
is used!
31
Exception Object (continued)
catch (System.Exception e)
{ Console.Error.WriteLine("Problem with scores - " +
"Can not compute average");
Console.Error.WriteLine(e.Message);
}
33
Exception Classes (continued)
• ApplicationException
• Derive from this class when you write your own exception classes
• User program must throw the exception, not the CLR
• SystemException
• Most run-time exceptions derive from this class
• SystemException class adds no functionality to classes; includes no
additional properties or methods
34
SystemException Class
35
SystemException Class (continued)
36
System.DivideByZeroException
37
Filtering Multiple Exceptions
38
Custom Exceptions
39
Custom Exceptions (continued)
40
public class TestOfCustomException
{
static void Main(string[] args)
{
double value1 = 0, value2=0, answer;
User- try
defined { //Could include code to enter new values.
class answer = GetResults(value1, value2);
}
catch (FloatingPtDivisionException excepObj)
{
Console.Error.WriteLine(excepObj.Message);
}
catch
{
Console.Error.WriteLine(“Something else happened!”);
}
}
41
Custom Exceptions (continued)
42
static double GetResults (double value1, double value2)
{
if (value2 < .0000001) // Be careful comparing floating-
// point values for equality.
{
FloatingPtDivisionException excepObj = new
FloatingPtDivisionException
(“Exceptionƒtype: “ +
“Floating-point division by zero”);
throw excepObj;
Throwing an
} exception
return value1 / value2;
}
}
43
Input Output (IO) Exceptions
• System.IO.IOException
• Direct descendent of Exception
• Thrown when there are problems loading or accessing the contents of a file
44
Input Output (IO) Exceptions (continued)
45
Chapter Summary
• Types of errors
• Debugger
• Halt execution to examine code
• Breakpoints
• Locals window shows variables in scope
• Step Into, Step Over, and Step Out
• Exceptions
• Unexpected conditions
• Abnormal termination if not handled
46
Chapter Summary (continued)
• Exceptions
• How to throw and caught exceptions
• Exception-handling techniques
• try…catch…finally clauses
• Exception classes
• Create custom Exception classes
• Throw exception
• Use multiple catch clauses
47