Docsity

Java Exception Handling: Understanding Exceptions, try-catch-throw, and Custom Exceptions, Lecture notes of Computer Networks

Partial preview of the text

Download Java Exception Handling: Understanding Exceptions, try-catch-throw, and Custom Exceptions and more Lecture notes Computer Networks in PDF only on Docsity!

Exception Handling

Program Errors

 Syntax (compiler) errors

 Errors in code construction (grammar, types)  Detected during compilation

 Run-time errors

 Operations illegal / impossible to execute  Detected during program execution  Treated as exceptions in Java

 Logic errors

 Operations leading to incorrect program state  May (or may not) lead to run-time errors  Detect by debugging code

What are Exceptions?

Many “exceptional” things can happen during the running of

a program, e.g.:

  • Bug in the actual language implementation
  • User mis-types input
  • Web page not available • File not found
    • Divide by zero
  • Array index out of bounds
  • Method called on a null object
  • Out of memory

Exceptions are unexpected conditions in programs.

checked unchecked sys errors

We can distinguish 3 categories:

  • checked exceptions — Problems to do with the program's

interaction with “the world”.

  • unchecked exceptions — Problems within the program

itself (i.e. violations of the contract, or bugs).

  • system errors — Problems with the underlying system.

These are outside our control.

The w happ These s by testin pro Unchecked Exceptions System Errors Program System The World Checked Exceptions

Using try and catch

 Handling an exception has two benefits,

 It allows you to fix the error

 It prevents the program from automatically terminating

 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 }

Using try and catch

 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

Uncaught Exceptions

 If an exception is not caught by user program, then execution

of the program stops and it is caught by the default handler

provided by the Java run-time system

 Default handler prints a stack trace from the point at which

the exception occurred, and terminates the program

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"

Example

Example (Cont.)

 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 " ); } 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 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 " ); } 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 That's all, folks

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 " ); } 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 { 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 " ); } 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)**