70 likes | 179 Views
exceptions. // Subclasses must precede superclasses in catch statements. class ExcDemo5 { public static void main(String args[]) { // Here, numer is longer than denom. int numer[] = { 4, 8, 16, 32, 64, 128, 256, 512 }; int denom[] = { 2, 0, 4, 4, 0, 8 };
E N D
exceptions // Subclasses must precede superclasses in catch statements. • class ExcDemo5 { • public static void main(String args[]) { • // Here, numer is longer than denom. • int numer[] = { 4, 8, 16, 32, 64, 128, 256, 512 }; • int denom[] = { 2, 0, 4, 4, 0, 8 }; • for(int i=0; i<numer.length; i++) { • try { • System.out.println(numer[i] +" / "+denom[i]+" is "+ numer[i]/denom[i]); • } • catch (ArrayIndexOutOfBoundsException exc) { • // catch the exception • System.out.println("No matching element found."); • } • catch (Throwable exc) { • System.out.println("Some exception occurred."); • } • } } } OUTPUT: 4 / 2 is 2 Some exception occurred. 16 / 4 is 4 32 / 4 is 8 Some exception occurred. 128 / 8 is 16 No matching element found. No matching element found. An example for a try with two catches one for arithmetic exceptions lines 10 to 13 ( divide by 0) and one for other exceptions 14 to 16 ( divide 256 by nothing)
exceptions The for body lines 9 to 12 so any error will be catched by the first catch lines 13 to 16 • class NestTrys { • public static void main(String args[]) { • // Here, numer is longer than denom. • intnumer[] = { 4, 8, 16, 32, 64, 128, 256, 512 }; • intdenom[] = { 2, 0, 4, 4, 0, 8 }; • try { // outer try • for(inti=0; i<numer.length; i++) { • try { // nested try • System.out.println(numer[i] + " / " + denom[i] + " is " + • numer[i]/denom[i]); • } • catch (ArithmeticExceptionexc) { • // catch the exception • System.out.println("Can't divide by Zero!"); • } • } • } • catch (ArrayIndexOutOfBoundsExceptionexc) { • // catch the exception • System.out.println("No matching element found."); • System.out.println("Fatal error -- program terminated."); • } • } • } OUTPUT: 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16 The second catch will only be entered only if there an error in line 8 (the for)
exception • // Rethrow an exception. • class Rethrow { • public static void genException() { • // here, numer is longer than denom • int numer[] = { 4, 8, 16, 32, 64, 128, 256, 512 }; • int denom[] = { 2, 0, 4, 4, 0, 8 }; • for(int i=0; i<numer.length; i++) { • try { • System.out.println(numer[i] + " / " + denom[i] + " is " + numer[i]/denom[i]); • } • catch (ArithmeticException exc) { • // catch the exception • System.out.println("Can't divide by Zero!"); • } • catch (ArrayIndexOutOfBoundsException exc) { • // catch the exception • System.out.println("No matching element found."); • throw exc; // rethrow the exception • } • } • } • } • class RethrowDemo { • public static void main(String args[]) { • try { • Rethrow.genException(); • } • catch(ArrayIndexOutOfBoundsException exc) { • // recatch exception • System.out.println(“array out of boundary."); • System.out.println("Fatal error "); • } • } • } OUTPUT: 4 / 2 is 2 Can't divide by Zero! 16 / 4 is 4 32 / 4 is 8 Can't divide by Zero! 128 / 8 is 16 No matching element found. Fatal error -- program terminated.
If we divide by 0 the error will be catched by the first catch lines (11 to 14) and not by the second catch because it is an arithmetic not an array out of boundary error. and the first catch will print “Can't divide by Zero!” If we reach number 256 there will be no number to divide it by because array “ denom” is out of boundary and the error will be catched by the second catch lines (15 to 19) and print “No matching element found.” and line 18 will throw an exception that will be catched in the main, lines (28 to 32) and print “array out of boundary.“ and "Fatal error”
exception • // Using the Throwable methods. • class ExcTest { • static void genException() { • int nums[] = new int[4]; • System.out.println("Before exception is generated."); • // generate an index out-of-bounds exception • nums[7] = 10; • System.out.println("this won't be displayed"); • } • } • class UseThrowableMethods { • public static void main(String args[]) { • try { • ExcTest.genException(); • } • catch (ArrayIndexOutOfBoundsException exc) { • // catch the exception • System.out.println("Standard message is: "); • System.out.println(exc); • } • System.out.println("After catch statement."); • } • } OUTPUT: Before exception is generated. Standard message is: java.lang.ArrayIndexOutOfBoundsException: 7 After catch statement.
exception What is the output when the following code is executed and the user enters abc123 and 14? • Scanner scanner = new Scanner(System.in); • try { • int num1 = scanner.nextInt(); • System.out.println("Input 1 accepted"); • int num2 = scanner.nextInt(); • System.out.println("Input 2 accepted"); • } catch (InputMismatchException e) { • System.out.println("Invalid Entry"); • } If the input is abc and 14 the output is Invalid Entry. If the input is 12 and 14 the outpot is Input 1 accepted Input 2 accepted
exception • Determine the output of the following code when the input “a” is entered. • Scanner scanner = new Scanner(System.in); • try { • int num = scanner.nextInt(); • if (num < 0) { • throw new Exception("No negative"); • } • } catch (InputMismatchException e) { • System.out.println("Invalid Entry"); • } catch (Exception e) { • System.out.println("Error: "+ e.getMessage()); • }} Answer: Invalid Entry