Professional Documents
Culture Documents
CSF213 L23,24
CSF213 L23,24
BITS Pilani
Hyderabad Campus
BITS Pilani
Hyderabad Campus
Exceptions
Todays Agenda
Exception Javas exception handling Exception handling hierarchy Exceptions Methods
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
Runtime exceptions
Errors
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
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 Hierarchy
All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class.
Exceptions Methods
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.
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.
Exception propagation
Method call stack
ExceptionProp.java ExceptionProp1.java
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
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
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
Ex1.java
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 "); } }
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