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 happ ish between them e.g.no pointers to deallocated memory Allow exeeptionsdp thendaalt with in a different place in the code from wberes threy"acur so we throw exceptions where thes osoilicagd oafct theem where we want to deal with them.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 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 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 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 42 99/42=2 That's all,folks 4
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 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 Usage:Etest <int><int> That's all,folks 5
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