









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
This document, presented in the context of a class taught by prof. Steven r. Lerman and dr. V. Judson harward, delves into error handling in java. The session focuses on the exception mechanism, exploring better and worse ways to handle errors. It covers error codes, exception handling using ejector seat, advantages of using exceptions, throwing and declaring exceptions, checked vs unchecked exceptions, creating exception instances, catching exceptions, and the throw/try/catch pattern. The document also covers catching exceptions up the call stack and not catching exceptions up the call stack, with examples in the context of a calculator application.
Typology: Exams
1 / 15
This page cannot be seen from the preview
Don't miss anything!
try { if ( error ) throw new MyException(); //skip futher execution
catch ( MyException e ) {
// handle exception e
catch( MyException2 e ) { ... } // optional
...
finally { // optional
... // always executed if present
//resume execution
Catching Exception Up the Call Stack
double a = 0.0;
try {
}
{
// do something about it
}
System.out.println( “Average = “ + a );
Not Catching Exception Up the Call Stack
double [] myDoubles = {... };
a = average( myDoubles );
catch ( IllegalArgumentException e )
import javax.swing.*;
public class BadArgument {
public static void main( String [] args ) { while ( true ) { String answer = JOptionPane.showInputDialog( "Enter an integer" ); int intAnswer = Integer.parseInt( answer ); // What happens if the user types %!Z$ if ( intAnswer == 42 ) break;
System.exit( 0 );
What an Exception Object Contains
String
method:
public String getMessage()
method that will print out the exception type and
the exception message, if any.
argument, if present) by using the
realization that the place you discover an error is
errors.
Exceptions and Error Handling
The Zen of error handling starts with the
almost never the place where you can fix it.
Older languages like C use the horrible kludge of
error codes and flags as we have seen.
C++ introduced exceptions into the C family.
But many programmers simply don’t test for
Exceptions in the Calculator, 4
programmer error.
Error
state until the user clears it.
is an
unchecked exception, it is caused here by user error (not entering enough operands) not
doesn't know how to fix it so it throws it.
doesn't know how to fix it, so
it throws it.
Finally,
something about it so it catches the exception and puts the whole calculator into the
Exceptions and Inheritance
A FileNotFoundException is a derived class of
IOException.
exception.
its superclasses.
classes, exception classes may use inheritance.
When an error is detected, you should create and
throw a new instance of an appropriate type of
statement matching the exception class or one of
Exception Inheritance Example
try
{ } { } { }
FileReader in = new FileReader( "MyFile.txt" ); // read file here
catch ( FileNotFoundException e )
// handle not finding the file (bad file name?)
catch ( IOException e )
// handle any other read error
BirthdayApp Exercise, 1
class.
Download Lecture25.zip from the class web site. These files implement a birthday list editor. Examine the classes in the 3 files: BirthdayApp : This is the application class with the main() method. BirthdayView : This is the combined view and controller
BirthdayModel : This class maintains the growing list of birthdays. Don't worry about the methods labelled "Related to MVC" towards the end of the file. They update the JList when a new birthday is added. Birthday : contains the name and date information.