270 likes | 406 Views
Applets & Applications. CSC 171 FALL 2001 LECTURE 15. 1954 - John Backus proposed the development of a programming language that would allow uses to express their problems in commonly understood mathematical formulae -- later to be named FORTRAN. History: FORTRAN. Change in Schedule.
E N D
Applets & Applications CSC 171 FALL 2001 LECTURE 15
1954 -John Backus proposed the development of a programming language that would allow uses to express their problems in commonly understood mathematical formulae -- later to be named FORTRAN History: FORTRAN
Change in Schedule • 11/12 – Chapter 14 – Exception Handling • 11/16 – Midterm – Hoyt – 8AM • 11/19 – Chapters 11, 12, & 13 • Graphics & GUIs (optional) • Enjoy the Turkey • 11/26 – Chapter 16 – Files & Streams • 11/29 – Project due • 12/3 – Chapter 19 – Data Structures • 12/11 – last lecture • 12/19 – FINAL EXAM - 12/19 4PM
Exam Friday 11/16 • 8 AM - 9 AM Hoyt • Chapters 1-10 – • end of chapter questions make good study materiel • Labs • Projects • Workshops • Multiple choice (20 @ 2%) • Some “Write a method/class that . . .” (10 @ 6%)
Throwing Exceptions • What should a method do when a problem is detected? • Traditionally, methods return some special code to indicate failure • However, • The caller may forget to check the return value • The caller may not be able to fix it
Problems with return signals • If the caller forgets to check • Bad data is processed String input = myTextBox.getText(); Double d = new Double(input); // what if I type “hello” i = d.doubleValue(); • If the caller can’t fix it • We have to punt to the caller’s caller x.doStuff(); // becomes If (!x.doStuff()) return false; //man, that’s a lot of code
Exception-handling mechanism to the rescue • Exceptions can’t be overlooked • Excepts can be handled by a competent handler • Not just the caller
Simple Try Block Syntax try{ statement; statement; } catch(ExceptionClass ExceptionObject){ statement; statement; }
Example: Console Input import java.io.*; public class ReadingDoubles { public static void main (String[] args) { double i, j ; try { // open the console for bufferd I/O InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader);
Alternately // instead of InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader); // could use nested constructors BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
What happens? public static void main (String[] args) { BufferedReader console = new BufferedReader(InputStreamReader(System.in)); System.out.print("Please enter a floating point number : "); String input = console.readLine(); Double d = new Double(input); double d1 = d.doubleValue(); }
This happens cd d:/courses/CSC171/CSC171FALL2001/code/ d:/devenv/jdk1.3/bin/javac Console1Bad.java Console1Bad.java:14: unreported exception java.io.IOException; must be caught or declared to be thrown String input = console.readLine(); //read a String ^ 1 error Compilation exited abnormally with code 1 at Mon Nov 12 21:14:52
Deal with the potential exception try { String input = console.readLine(); } catch (IOException e) { System.out.println(e + “bad read”); System.exit(0); }
Example //get the value System.out.print("Please enter a floating point number : "); try { input = console.readLine(); //read a String } catch (IOException e) { System.out.println(e + " bad read "); System.exit(0); }
Now check the number format try{ Double d = new Double(input); double d1 = d.doubleValue(); // Calculate the sum & printout System.out.println(d1 + "^2 == " + (d1*d1)); } catch (NumberFormatException e) { System.out.println(e + " : What you entered was not a double"); }
Checked & Unchecked Exceptions • Java Exceptions fall into two categories • Checked • You MUST tell the compiler what you are going to do about the exception • Unchecked (sub-classes of RuntimeException) • NumberFormatException • IllegalArgumentException • NullPointerException
Why Checked & Unchecked? • Checked Exceptions are not your fault • So, you have to deal with them • Unchecked Exceptions are your fault • So, we trust you to deal with them • You have to deal with things you cannot prevent!
Cheap hacks • Lazy empty clauses try {System.in.read()} catch (IOException e){} • Punt the exception to the caller public static void main(String[] args) throws IOException
Alternate version I import java.io.*; public class ReadingDoubles3{ public static void main (String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in); BufferedReader console = new BufferedReader(reader); //get the first value System.out.print("Please enter a floating point number : "); String input = console.readLine(); //read a String Double d = new Double(input); double d1 = d.doubleValue();
Alternate Version II // Calculate the sum & printout System.out.println(d1 + "^2 == " + (d1*d1)); // wait for user to end System.out.println(" Hit return to exit"); System.in.read(); } }
Throwing your own exeptions public static double myfun(double ValueBetweenZeroAndOne) { if ((ValueBetweenZeroAndOne < 0) || ( ValueBetweenZeroAndOne > 1)) throw new IllegalArgumentException("RTFM - you dolt!"); return ValueBetweenZeroAndOne * ValueBetweenZeroAndOne; }
Making them deal with it public static double myfun(double ValBetZeroAndOne) throws Exception { // now, they have to write a handler if ((ValBetZeroAndOne < 0) || ( ValBetZeroAndOne > 1)) throw new IllegalArgumentException("RTFM – you dolt!"); return ValBetZeroAndOne * ValBetZeroAndOne; }
Defining your own exceptions public class DivideByZeroException extends RuntimeException { public DivideByZeroException() { super(“Attempt to divide by Zero”); } public DivideByZeroException( String msg) { super(msg); } }
Defining your own checked exceptions public class myCheckException extends Exception { public myCheckException() { super(“Attempt to divide by Zero”); } public myCheckException( String msg) { super(msg); } }
Finally try{ statements; } catch(ExceptionClass ExceptionObject){ statements; //exception specific } finally { statements; //clean up }
Finally • The Finally block executes regardless of what happens in the try/catch • Good for cleaning up resources • Good for conditions where the catch blocks may throw exceptions.