public class Etest public static void main(String args[]){ /What we expect to happen try int x Integer.parseInt(args[0]); int y Integer.parseInt(args[1]); System.out.println(x +"/+y +"="x/y ) /Things which can go wrong catch (IndexOutofBoundsException e){ System.out.println("Usage:Etest <int><int>"); catch (NumberFormatException e){ System.out.println(e.getMessage()+"is not a number"); /Do this regardless finally System.out.println "That's all,folks") }//main }// Etest java Etest 99 fred fred is not a number That's all,folks 6
16 public class Etest { public static void main(String args[]){ // What we expect to happen System.out.println( x + "/" + y + " = " + x/y ); } try { int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); catch (NumberFormatException e) { // Things which can go wrong catch (IndexOutOfBoundsException e) { System.out.println( "Usage: Etest <int> <int>" ); } System.out.println( e.getMessage() + " is not a number" ); } // Do this regardless finally { System.out.println( "That's all, folks" ); } } // main } // Etest > java Etest 99 fred fred is not a number That's all, folks
public class Etest public static void main(String args[]){ /What we expect to happen try int x Integer.parseInt(args[0]); int y Integer.parseInt(args[1]); System.out.println(x +"/+y +"="x/y ) /Things which can go wrong catch (IndexOutofBoundsException e){ System.out.println("Usage:Etest <int><int>"); catch (NumberFormatException e){ System.out.println(e.getMessage()+"is not a number"); /Do this regardless finally System.out.println "That's all,folks") }//main }// Etest java Etest fred fred is not a number That's all,folks
17 public class Etest { public static void main(String args[]){ // What we expect to happen System.out.println( x + "/" + y + " = " + x/y ); } try { int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); catch (NumberFormatException e) { // Things which can go wrong catch (IndexOutOfBoundsException e) { System.out.println( "Usage: Etest <int> <int>" ); } System.out.println( e.getMessage() + " is not a number" ); } // Do this regardless finally { System.out.println( "That's all, folks" ); } } // main } // Etest > java Etest fred fred is not a number That's all, folks
public class Etest public static void main(String args[]){ /What we expect to happen try int x Integer.parseInt(args[0]); int y Integer.parseInt(args[1]); System.out.println(x +"/+y +"="x/y ) /Things which can go wrong catch (IndexOutofBoundsException e){ System.out.println "Usage:Etest <int><int>"); catch (NumberFormatException e){ System.out.println(e.getMessage()+"is not a number"); /Do this regardless finally System.out.println("That's all,folks"); }//main }// java Etest 99 0 Etest That's all,folks java.lang.ArithmeticException:by zero at Etest.main (Etest.java:8) 8
18 public class Etest { public static void main(String args[]){ // What we expect to happen System.out.println( x + "/" + y + " = " + x/y ); } try { int x = Integer.parseInt(args[0]); int y = Integer.parseInt(args[1]); catch (NumberFormatException e) { // Things which can go wrong catch (IndexOutOfBoundsException e) { System.out.println( "Usage: Etest <int> <int>" ); } System.out.println( e.getMessage() + " is not a number" ); } // Do this regardless finally { System.out.println( "That's all, folks" ); } } // main } // Etest > java Etest 99 0 That's all, folks java.lang.ArithmeticException: / by zero at Etest.main(Etest.java:8)
Using finally for Cleanup Finalizers aren't much good for releasing resources To get guaranteed cleanup of rk connections etc. use finally because we don't know when (or Socket s; even if)they will be called Inputstream in; try s=new Socket(.·.); in=s.getInputstream();·· J finally try if (in !null)in.close()); }s.close(); catch (IOException e){} So we actually need a try...catch.blggkewichid thes finally clause down at the wrong moment 19
19 Using finally for Cleanup Finalizers aren't much good for releasing resources To get guaranteed cleanup of network connections etc. use finally clauses, e.g.: because we don't know when (or even if) they will be called But there's a snag — the call to in.close() may itself throw an exception e.g. if the network goes down at the wrong moment So we actually need a try ... catch block within the finally clause Socket s; InputStream in; try { s = new Socket(...); ... in = s.getInputStream(); ... catch (IOException e) {...} } finally { if (in != null) in.close()); s.close(); } s.close(); finally { try { if (in != null) in.close()); } } catch (IOException e){}
Creating and Throwing Your Own Exceptions You can create your own exception classes by inheriting from the standard ones. E.g. class UnhappyWidgetException extends Exception public UnhappyWidgetException (Widget w){ super (w.getProblem()); Whe The constructor should call the you superclass constructor with a string t. to indicate what went wrong 20
20 Creating and Throwing Your Own Exceptions You can create your own exception classes by inheriting from the standard ones. class UnhappyWidgetException extends Exception { public UnhappyWidgetException (Widget w) { super(w.getProblem()); } E.g. When the relevant problem is detected, you create an exception and throw it. The constructor should call the superclass constructor with a string to indicate what went wrong