0% found this document useful (0 votes)
33 views11 pages

Exception Handling (Lecture 9.1)

Uploaded by

Junaid Qaisar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views11 pages

Exception Handling (Lecture 9.1)

Uploaded by

Junaid Qaisar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lecture Outline:

 try & catch blocks


 Multiple catch clauses and nested try statements
 Throw, throws, finally
 Creating own Custom exceptions

CS-3130 Advance Object Oriented Programming (2013) 1


Using try & catch:
 As mentioned earlier, Exceptions can be handled or debugged either in
two ways:

1. By default exception handler provided by the Java run-time system.


2. Manually by the programmer through catch keyword within your code.

 There are two main benefits of Debugging or Handling an exception


manually by the programmer:

1. Allows you to fix the error.


2. Prevents the program from automatic termination.

CS-3130 Advance Object Oriented Programming (2013) 2


Using try & catch (Contd):
 To handle an exception or a runtime error manually, the following steps are
required to be followed by the relevant programmer:

 Enclose the code inside a try block that you want to monitor or which can
possibly activate an exception.
 Include a catch clause right after the try block. The catch clause specifies
the exception type that you wish to catch.

 For instance, consider the previous program that process an


ArithmeticException generated by the division-by-zero error:

Continued to next slide…..

CS-3130 Advance Object Oriented Programming (2013) 3


Using try & catch (Contd):
 class Exc2 {
 public static void main(String args[]) {
 int d, a;
 try { // monitor a block of code. PROGRAM OUTPUT
 d = 0; Division by zero.
 a = 42 / d; After catch statement.
 System.out.println("This will not be printed.");
 } catch (ArithmeticException e) { // catch divide-by-zero error
 System.out.println("Division by zero.");
 }
 System.out.println("After catch statement.");
 }

 Once an exception is thrown, program control transfers from the try block into the catch
block.

CS-3130 Advance Object Oriented Programming (2013) 4


Displaying a Description of an Exception:
 One can display the description of an exception in a println( ) statement
by simply passing the exception as an argument.
 For example, the catch block in the previous program can be rewritten
like this:

 catch (ArithmeticException e) {
 System.out.println("Exception: " + e);
 }
 An argument e of built in Exception type ArithmeticException is being
passed as an argument in the println statement.
 Moreover, the following message would be displayed on execution of the
program:

Exception: java.lang.ArithmeticException: / by zero

CS-3130 Advance Object Oriented Programming (2013) 5


Multiple catch clauses:
 In some cases, multiple exceptions could be raised by a single piece of
code.
 To handle this type of situation, one can define two or more catch clauses,
each catching a different type of exception.
 When an exception is thrown, each catch statement is inspected in order,
and the first one whose type matches that of the exception is executed.
 The following example catch two different exception types:

 // Demonstrate multiple catch statements.


 class MultiCatch {
 public static void main(String args[]) {
 try {
 Continued to next slide….

CS-3130 Advance Object Oriented Programming (2013) 6


Multiple catch clauses (contd):
 int a = args.length;
PROGRAM OUTPUT
 System.out.println("a = " + a); Divide by 0: java.lang.ArithmeticException: / by zero
 int b = 42 / a;
 int c[] = { 1 }; Array index oob:
 c[42] = 99; java.lang.ArrayIndexOutOfBoundsException: 42
 } catch(ArithmeticException e) {
 System.out.println("Divide by 0: " + e);
 } catch(ArrayIndexOutOfBoundsException e) {
 System.out.println("Array index oob: " + e);
 }
 System.out.println("After try/catch blocks.");
 }
 }
 This program will create two types of exceptions:
 An ArithmeticException since the value is divided by zero.
 An ArrayIndexOutOfBoundsException, since the int array c has a length of 1, yet the
program attempts to assign a value to c[42].

CS-3130 Advance Object Oriented Programming (2013) 7


Nested try statements:
 The try statement can be nested.
 A try statement can be inside the block of another try.
 If an inner try statement does not have a catch handler for a particular exception, the stack is
unwound and the next try statement’s catch handlers are inspected for a match. This continues
until one of the catch statements succeeds, or until all of the nested try statements are
exhausted.
 If no catch statement matches, then the Java run-time system will handle the exception.

 See Programming Example at Page 211, 212, Java, The complete reference.

CS-3130 Advance Object Oriented Programming (2013) 8


Throw:
 In the previous programs, we have caught exceptions which were automatically thrown by the
Java runtime system.
 However, a program can throw an exception manually or explicitly as well.
 This can be done by using the throw statement.

 The general syntax of throw statement is mentioned below:

 throw ThrowableInstance;
 whereas,
 ThrowableInstance must be an object of type Throwable or a subclass of Throwable.

 There are two ways to declare a Throwable object:


1) using a parameter in a catch clause.
2) Creating one with the new operator.

CS-3130 Advance Object Oriented Programming (2013) 9


Throw (Flow of execution):
 The execution of the program terminates immediately after the throw statement.
 The nearest enclosing try block is inspected to see if it has a catch statement that matches the
type of exception.
 If it does find a match, control is transferred to that statement. If not, then the next enclosing
try statement is inspected, and so on.
 If no matching catch is found, then the default exception handler halts the program and
prints the stack trace.

CS-3130 Advance Object Oriented Programming (2013) 10


Throw (Programming Example):
 Here is a sample program that creates and throws an exception. The handler that catches
 the exception rethrows it to the outer handler.
 // Demonstrate throw. new is used to construct an instance
ofNullPointerException.
 class ThrowDemo { Many of Java’s builtin run-time exceptions have at least
 static void demoproc() { two constructors: one with no parameter and one that
 try { takes a string parameter.
 throw new NullPointerException("demo");
 } catch(NullPointerException e) {
 System.out.println("Caught inside demoproc.");
 throw e; // rethrow the exception } }
 public static void main(String args[]) {
 try {
 demoproc();
 } catch(NullPointerException e) {
 System.out.println("Recaught: " + e); } }

CS-3130 Advance Object Oriented Programming (2013) 11

You might also like