What do we want of exceptions? Ideally, a language(and its implementation) should Restrict the set of possible exceptions to reasonable"ones Indicate where they happed ane. ch between them e.g. no pointers to deallocated memory Allow exceatieasntp thendealt with in a different place in the code from wheRes tereyr"Gtcur soWethr。 w exceptions where fhe RoS:F品8gr where we want to deal with therh. written cleanly without having Ideally, we don't want non-fatal exceptions to be thrown too far-this breaks up the modularity of the program and makes it hard to reason about
11 What do we want of exceptions? Ideally, a language (and its implementation) should: • Restrict the set of possible exceptions to “reasonable” ones • Indicate where they happened, and distinguish between them e.g. no pointers to deallocated memory and not map them all to “bus error” etc. • Allow exceptions to be dealt with in a different place in the code from where they occur so normal case code can be written cleanly without having to worry about them so we throw exceptions where they occur, and catch them where we want to deal with them. Ideally, we don't want non-fatal exceptions to be thrown too far — this breaks up the modularity of the program and makes it hard to reason about
Exceptions in Java-a reminder In Java, the basic exception handling construct is to try a block of code which normally executes ok catch any exceptions that it generates, and finally do anything we want to do irrespective of what happened before If a thrown exception is not caught, it propagates out to the caller and so on until main If it is never caught, it terminates the program If a method can generate(checked) exceptions but does not handle them, it has to explicitly declare that it throws them so that clients know what to expect 12
12 Exceptions in Java — a reminder In Java, the basic exception handling construct is to: If a thrown exception is not caught, it propagates out to the caller and so on until main. If a method can generate (checked) exceptions but does not handle them, it has to explicitly declare that it throws them so that clients know what to expect. • finally do anything we want to do irrespective of what happened before. • catch any exceptions that it generates, and If it is never caught, it terminates the program. • try a block of code which normally executes ok
EXample public class etest i public static void main(string args[])i 7/ What we expect to happen try i int x Integer parseInt(args[o])i int y Integer parseInt(args[1])i System. out. println(x+"/+y+W=l+x/y );] 7/Things which can go wrong catch (IndexOutofBoundsException e) i System. out. println( "Usage: Etest <int> <int>l )i 1 catch (NumberFormatException e) i System. out. println(e getMessage()+ "l is not a number // Do this regardless finally i System. out. println( " That's all, folks")i 1 3// main ]//Etest 13
13 Example 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
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 42 99/42=2 Thats all, folks
14 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 42 99/42 = 2 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 99 Usage: Etest <int> <int> Thats all folks
15 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 Usage: Etest <int> <int> That's all, folks