public class Etest i public static void main(string args[])i //What we expect to happen try t int x Integer parseInt(args [o])i int y Integer parseInt(args[1])i System. out. println( x+"/+y+l =n+ x/y )i] // Things which can go wrong catch (IndexOutofBoundsException e) System. out. println( "Usage: Etest <int> <int>l)i) catch (NumberFormatException e) i System. out. println(e getMessage()+ " is not a number )i 7/ Do this regardless finally t System. out. println("That's all, folks" )i J 3// main 3 //Etest Java Etest 99 fred fred is not a number Thats all folks
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 i public static void main(string args[])i //What we expect to happen try t int x Integer parseInt(args [o])i int y Integer parseInt(args[1])i System. out. println( x+"/+y+l =n+ x/y )i] // Things which can go wrong catch (IndexOutofBoundsException e) System. out. println( "Usage: Etest <int> <int>l)i) catch (NumberFormatException e) i System. out. println(e getMessage()+ " is not a number )i 7/ Do this regardless finally t System. out. println("That's all, folks" )i J 3// main 3 //Etest Java Etest fred fred is not a number Thats 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 i public static void main(string args[])i //What we expect to happen try t int x Integer parseInt(args [o])i int y Integer parseInt(args[1])i System. out. println( x+"/+y+l =n+ x/y )i] // Things which can go wrong catch (IndexOutofBoundsException e) System. out. println( "Usage: Etest <int> <int>l)i) catch (NumberFormatException e) i System. out. println(e getMessage()+ " is not a number )i 7/ Do this regardless finally t System. out. println("That's all, folks" )i J // main 3 //Etest java Etest 99 0 That'sa11,f。1ks java. lang ArithmeticException: by zero at Etest. main(etest. java: 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 I ark connections etc. use finally because we don't know when(or Socket si even if) they will be called Inputstream in try i s= new Socket(.)i n=s. getInputstream();…· finally i try t if (in ! null) in close())i s.c1。se(); catch (IOException e)i] So we actually need a try . catch. g! aake rth goes 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(); 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 i public Unhappy WidgetException (Widget w) t super (w. getProblemo)) Whe The constructor should call the you d superclass constructor with a string t to indicate what went wrong
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