Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 29

BITS Pilani

BITS Pilani
Hyderabad Campus

Dr.Aruna Malapati Asst Professor Department of CSIS

BITS Pilani
Hyderabad Campus

Exceptions

Todays Agenda
Exception Javas exception handling Exception handling hierarchy Exceptions Methods

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Exception
An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following:
A user has entered invalid data. A file that needs to be opened cannot be found. A network connection has been lost in the middle of communications, or the JVM has run out of memory.

Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Three categories of exceptions


Checked exceptions

Runtime exceptions
Errors

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

How to Handle Errors


Error must be handled where they occur. Ignore: False alarm just continue. Report: Write a message to the screen or to a log. Terminate: Stop the program execution.

Repair: Make changes and try to recover the error.


However, often the best that can be done is the combination of report and terminate.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Java's Exception Handling


Exception: An event that occurs during the execution of a program the disrupts the normal transaction flow.
A run-time phenomenon.

Exception handling is part of the language. Exceptions are objects. Exceptions are structured in a class hierarchy. It is not possible to ignore an exceptions (very nice feature).
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Java's Exception Handling, cont.


Javas object-oriented way to handle errors
more powerful, more flexible than using return keywords try, catch, throw, throws, finally.

An exception is an object that describes an erroneous or unusual situation. Exceptions are thrown by a program, and may be caught and handled by another part of the program. A program can therefore be separated into a normal execution flow and an exception execution flow.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Exception Handling Model


Code where you anticipate a problem public static void main (String args[]) throws exception1, exception2, exception3 { ... }

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Exception Hierarchy
All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Exceptions Methods

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Unchecked built in exceptions


Java defines several exception classes inside the standard package java.lang Java Unchecked RuntimeException

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Java Checked Exceptions


Following is the list of Java Checked Exceptions Defined in java.lang.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Catching Exceptions
A method catches an exception using a combination of the try and catch keywords. try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Exception handling techniques


The try/Catch blocks Multiple catch blocks The try-with-resources statements Nested try blocks
(Except1.java,Except2.java, ExcepTest.java)

The throw clause


The throws clause
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

syntax
try { //code that might cause exception } catch(ExceptionName e1) { //When exception occurs the code come here } finally { // release some resources here }
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

try/catch rules
When there is a try block, catch or finally block should be there. There should not be any statements between try and catch and finally. Finally block is optional and will be executed regardless of whether an exception is raised or not. Multiple catches allowed but single finally block allowed.

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Exception propagation
Method call stack

ExceptionProp.java ExceptionProp1.java

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Multiple catch blocks


try { //Protected code } catch(ExceptionType1 e1) { //Catch block } catch(ExceptionType2 e2) { //Catch block } catch(ExceptionType3 e3) { //Catch block }
Example: Multi_Catch.java
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Using nested try blocks


try { statement 1; statement 2; try { statement 1; statement 2; } catch(Exception e) {} } catch(Exception e) {}
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

The throw clause

factdemo.java

To throw an exception, we use the throw statement. Syntax: throw exceptionObject ; exceptionObject is an object of the class Exception (or of one of its subclasses) Example: throw new MyException("message for MyException"); This statement throws an exception of type MyException, which prints on the screen the message that appears as parameter to the constructor.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Using throws clause


Throws keyword is used to explicitly throw an exception. We can throw a checked or unchecked exception. When an exception is not handled, a method can declare the exception with throws clause. When marked as throws, the calling methods have to handle the exception or declare to throw the same.
ThrowsEx.java ThrowsEx1.java
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Differences between throw and throws

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

User defined exception


It is possible to throw your own exceptions and throw them when they occur. Such user defined exceptions classes must first defined as a subclass of the exception class. Ex class student extends Exception

For any user defined class, we can use all methods of Throwable class.
NegativeAgeException.java
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

Examples
What will be the output of the program? public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } } }

Finally

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Example
What will be the output of the program? try { int x = 0; int y = 5 / x; } catch (Exception e) { System.out.println("Exception"); } catch (ArithmeticException ae) { System.out.println(" Arithmetic Exception"); } System.out.println("finished");
CS/IS F213 First Semester 2012-13

Compile time error

Ex1.java

BITS Pilani, Hyderabad Campus

Example
What will be the output of the program? public class X { public static void main(String [] args) { try { badMethod(); System.out.print("A"); } catch (Exception ex) { System.out.print("B"); } finally { System.out.print("C"); } System.out.print("D"); } public static void badMethod() { throw new Error(); /* Line 22 */ } } CS/IS F213 First Semester 2012-13

BDE
BITS Pilani, Hyderabad Campus

Example
public class RTExcept { public static void throwit () { System.out.print("throwit "); throw new RuntimeException(); } public static void main(String [] args) { try { System.out.print("hello "); throwit(); } catch (Exception re ) { System.out.print("caught "); } finally { System.out.print("finally "); } System.out.println("after "); } }

hello throwit caught finally after

CS/IS F213 First Semester 2012-13

BITS Pilani, Hyderabad Campus

Summary
Exception is an error at run time that can be avoided if programmer properly handles it. Exception are checked or unchecked. Use of try, catch, throw and throws are Exception handling mechanisms.

If the programmer does not take care the JVM identifies the problem and prints some message that cannot be understood.
CS/IS F213 First Semester 2012-13 BITS Pilani, Hyderabad Campus

You might also like