



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































Errors in code construction (grammar, types) Detected during compilation
Operations illegal / impossible to execute Detected during program execution Treated as exceptions in Java
Operations leading to incorrect program state May (or may not) lead to run-time errors Detect by debugging code
checked unchecked sys errors
The w happ These s by testin pro Unchecked Exceptions System Errors Program System The World Checked Exceptions
Handling an exception has two benefits,
The catch clause should follow immediately the try block Once an exception is thrown, program control transfer out of the try block into the catch block Once the catch statement has executed, program control continues with the next line in the program following the entire try/catch mechanism
Exception-Handling Fundamentals Any code that absolutely must be executed before a method returns is put in a finally block General form of an exception-handling block try{ // block of code to monitor for errors } catch ( ExceptionType1 exOb ){ // exception handler for ExceptionType } catch ( ExceptionType2 exOb ){ // exception handler for ExceptionType } //… finally{ // block of code to be executed before try block ends }
A try and catch statement form a unit. The scope of the catch clause is restricted to those statements specified by the immediately preceding try statement
Ex: class Exc0 { public static void main(String args[]) { int d = 0; int a = 42 / d; } } Output: java.lang.ArithmeticException: / by zero at Exc0.main(Exc0.java:4) Exception in thread "main"
If no command line argument is provided, then you will see the following output: a = 0 Divide by 0: java.lang.ArithmeticException: / by zero After try/catch blocks If any command line argument is provided, then you will see the following output: a = 1 Array index oob: java.lang.ArrayIndexOutOfBoundsException After try/catch blocks.
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
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
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
**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
java Etest 99 0 That's all, folks java.lang.ArithmeticException: / by zero at Etest.main(Etest.java:8)**