








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
You must complete the project by yourself
Assistance can only be provided by teaching assistants(TAs) and instructors
You must not look at other students' code
Read entire assignment from beginning to end beforestarting to code
Check out assignment now from CVS
Follow the instructions exactly , as much of grading is automated
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
If program generates (“throws”) exception then default behavior is: z
z
Example
z
z
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Aborting program not always a good idea z
z Java includes mechanisms for recoveringfrom exceptions
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
They are created from classes
The classes are derived (“inherit”) from a special class,Throwable
We will learn more about inheritance, etc., later
Exception(String message) Constructor taking an explanation as an argument
String getMessage() Method returning the embedded message of the exception
void printStackTrace() Method printing the call stack when the exception was thrown
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
Exceptions are objects z In Java, objects are created from classes z Classes are also types z Are there exception classes in Java? Yes! Some examples from theJava class library (mostly java.lang): z ArithmeticException : Used e.g. for divide by zero
NullPointerException : attempt to access an object with a null reference
IndexOutOfBoundsException : array or string index out of range
ArrayStoreException : attempting to store wrong type of object in array
EmptyStackException : attempt to pop an empty Stack (java.util)
IOException : attempt to perform an illegal input/output operation (java.io)
NumberFormatException : attempt to convert an invalid string into a number (e.g., when calling Integer.parseInt( ) )
RuntimeException : general run-time error (subsumes above)
Exception : The most generic type of exception
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
z
z
4
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
In previous example: z Exception thrown in one method (findIndex) … z … but caught in another (main)
Java uses exception propagation to look for exception handlers z When an exception occurs, Java pops back up the call stack toeach of the calling methods to see whether the exception is beinghandled (by a try-catch block). This is exception propagation z The first method it finds that catches the exception will have itscatch block executed. Execution resumes normally in the methodafter this catch block z If we get all the way back to main and no method catches thisexception, Java catches it and aborts your program
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
class EasyExample { public static void g(boolean b) { System.out.println ("Entering g ..."); if (b) throw new RuntimeException("Oh no, you really messed up..."); System.out.println ("Exiting g ..."); } public static void f(int i) { System.out.println ("Entering f ..."); g(i == 0); System.out.println ("Exiting f ..."); } public static void main(String[] args) { try { f(1); } catch (RuntimeException e) { System.out.print ("Runtime error. Message: "); System.out.println (e.getMessage()); } } }
CMSC 131 Spring 2007 Bonnie Dorr (adapted from Rance Cleaveland)
public static void main(String[] args) { String d = ""; try { d = JOptionPane. showInputDialog ("Enter date: (mm/dd/yyyy)"); int year = getYear (d); System. out .println("The year is "
year); } catch (IndexOutOfBoundsException e) { System. out .println("index error"); } catch (NumberFormatException e) { System. out .println("Number format problem"); return ; // NOTE: finally block will still run!! } finally { System. out .println("Finally block is running now..."); } }