0% found this document useful (0 votes)
55 views

5 Exception Handling

This document discusses exception handling in Java. It covers the try, catch, throw, throws, and finally keywords used to handle exceptions. The try block monitors code for errors. The catch block handles specific exceptions. The throw keyword manually throws an exception, while throws specifies exceptions a method can throw. Finally, code in the finally block is always executed before the method returns.

Uploaded by

Abdi saba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

5 Exception Handling

This document discusses exception handling in Java. It covers the try, catch, throw, throws, and finally keywords used to handle exceptions. The try block monitors code for errors. The catch block handles specific exceptions. The throw keyword manually throws an exception, while throws specifies exceptions a method can throw. Finally, code in the finally block is always executed before the method returns.

Uploaded by

Abdi saba
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Exception Handling

Chapter Five
Exception-Handling Fundamentals

 Java exception handling is managed via five keywords:


 try,
 catch,
 throw,
 throws, and
 finally.
 System-generated exceptions are automatically thrown by the Java run-time system.
 To manually throw an exception, use the keyword throw.
 Any exception that is thrown out of a method must be specified as such by a throws
clause.
 Any code that absolutely must be executed before a method returns is put in a finally
block.
Cont….

 This is the general form of an exception-handling block:


try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
// ...
 
finally
{
// block of code to be executed before try block ends
}
Exception Types:

 try and catch


 throw
 throws
 finally
try and catch

 thefollowing program includes a try block and a catch clause which processes the Arithmetic Exception generated
by the division-by-zero error:

class tryAndCatch
{
public static void main(String args[])
{
int d, a;
try
{ // monitor a block of code.
d = 0;
a = 42 / d;
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.");
}
}
throw

 It is possible to throw an exception explicitly, using the throw statement


other than catching exceptions that are thrown by the Java run-time system.
 The general form of throw is shown here:
 throw ThrowableInstance;
 Here, ThrowableInstance must be an object of type Throwable or a subclass
of Throwable.
Cont….

class ThrowDemo {
static void demoproc() {
try
{
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);
}}}
Cont....

 The program gets two chances to deal with the same error.
 First, main( ) sets up an exception context and then calls demoproc( ).
 The demoproc( ) method then sets up another exception-handling context
and immediately throws a new instance of NullPointerException, which is
caught on the next line, then exception is rethrown.
 Output : 
Caught inside demoproc.
Recaught: java.lang.NullPointerException: demo
throws

class ThrowsDemo
{
static void throwOne() throws IllegalAccessException
{
System.out.println("Inside throwOne.");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try {
throwOne();
}
catch (IllegalAccessException e)
{
System.out.println("Caught " + e);
}
}
}
 
 Output :
 
inside throwOne
caught java.lang.IllegalAccessException: demo
finally

 finally creates a block of code that will be executed after a try/catch block
has completed and before the code following the try/catch block.
 The finally block will execute whether or not an exception is thrown.
 If an exception is thrown, the finally block will execute even if no catch
statement matches the exception.
 The finally clause is optional. However, each try statement requires at least
one catch or a finally clause.
Cont….

 Here is an example program that shows three methods that exit in various ways, none without executing
their finally clauses:
 Example : 
class FinallyDemo
{
// Through an exception out of the method.
static void procA()
{
try
{
System.out.println("inside procA");
throw new RuntimeException("demo");
}
Finally
{
System.out.println("procA's finally");
}
}
Cont….

// Return from within a try block.


static void procB()
{
try
{
System.out.println("inside procB");
return;
}
finally
{
System.out.println("procB's finally");
}
}
Cont….

// Execute a try block normally.


static void procC()
{
try
{
System.out.println("inside procC");
}
finally
{
System.out.println("procC's finally");
}
}
Cont….

public static void main(String args[])


{
try
{
procA();
}
catch (Exception e)
{
System.out.println("Exception caught");
}
procB();
procC();
}
}
Cont….

 In this example, procA( ) prematurely breaks out of the try by throwing an exception.
The finally clause is executed on the way out. procB( )’s try statement is exited via a
return statement. The finally clause is executed before procB( ) returns. In procC( ),
the try statement executes normally, without error. However, the finally block is still
executed.
 If a finally block is associated with a try, the finally block will be executed upon
conclusion of the try.
 Output :
inside procA
procA’s finally
Exception caught
inside procB
procB’s finally
inside procC
procC’s finally
THANK YOU

Any Questions?

You might also like