******************************************************************************* 0) Administrative - hw: read 8.3-8.4 - questions about A3? 1) Last time- finished strings - quiz for chapter 9 2) Go over 8.1-8.2 in your groups 3) Highlights A. What is an exception? - an object that represents a runtime error B. Why do exceptions happen? -bad input, outside problems, code errors - you want your code to NOT crash, no matter what - so you fix code errors and use exception handling for the others - this lets your program recover gracefully when problems occur C. What happens normally, if you don't do anything special about them public int parse() { return Integer.parseInt("one"); // leave space } public ... main(...) { int one = parse(); // leave space } - a NumberFormatException object is created and thrown by the system - once "thrown," it must be "caught" or program will crash - since method() doesn't catch it, it propagates up to main - since main doesn't catch it, the program crashes D. How can you catch it? -put a try-catch statement in either place - the code that causes the exception goes in the try block - you specify the type of exception you're watching for - if exception occurs, code in catch block gets executed - either way, code continues past catch block after (or returns) - can do this (once) anywhere along path E. About exception objects - "e" is like a parameter; a temp name for the exception object - you can call methods: S.o.p(e.getMessage()), e.printStackTrace() - that gives you the output you normally see when it crashes Ex1: Use exceptions so that if the denominator is 0, the result is 0. Dividing by 0 throws an ArithmeticException. public int divide(int num, int denom) { try { return num/denom; } catch (ArithmeticException e) { return 0; } } - show what happens step-by-step F. You can put multiple catch blocks to watch for more than one try { return Integer.parseInt(bufReader.readLine()); } catch (NumberFormatException e) { return 0; } catch (IOException e) { System.exit(0); } - that one line could cause 2 exceptions, and this handles both - the first matching catch block is executed, the rest ignored - what if none match? -propagates, as if no try-catch at all Note: - if you do this, no more "throws IOException" in header - must say throws until you catch, if ever, for IO (special kind) - can do neither for NF (but now that you know it, you should catch) Ex2: This inputs a name and return it in lower-case. The getString() method could throw an IOException (quit), or it could return null, causing a NullPointerException at the toLowerCase() call (repeat). public String getName() { while (true) { try { S.o.p("Name: "); return getString().toLowerCase(); } catch (IOException e) { System.exit(0); } catch (NullPointerException e) { S.o.p("Invalid entry."); } } } - just repeating input in catch only works once (2nd crashes!) - have to put a loop around all of it to repeat correctly - show what happens step-by-step G. To catch a type, the parameter type can be exact or more general - inheritance; NFE and IO are subclasses of Exception (all are) - substitute with Exception, and any kind will be caught - but careful with order, b/c later blocks will never execute H. The finally clause - if try block starts, this runs with or without an exception - happens even if there's a return inside the catch block I. Throwing your own exception - if you want to define something to be an error that isn't normally - create an Exception object and throw it throw new Exception("error message"); Ex3: Return a non-negative number from input, without breaking public int getAge() { while (true) { try { S.o.p("Number: "); int x = Integer.parseInt(getString()); if (x < 0) throw new Exception("No negative numbers."); return x; } catch (NumberFormatException e) { S.o.p("Not a number."); } catch (IOException e) { System.exit(0); } catch (Exception e) { S.o.p(e.getMessage()); } } } - show what happens step-by-step - remember you don't have to handle all of these right here; could have one thrown and handle it around the getAge() call. 5) Summary - handling exceptions - making programs unbreakable *******************************************************************************