Presentation Description

bca 5th sem BU


By: karthikshankar31 (107 month(s) ago)

im also frm blore..

Presentation Transcript



Exception-Handling Fundamentals:

Exception-Handling Fundamentals Java exception handling is managed by five keywords: try, catch, throw, throws, and finally Program statements to monitor are contained within a try block. If an exception occurs within the try block, it is thrown. Code within catch block catch the exception and handles it. 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

Coding Exceptions:

Coding Exceptions Try-Catch Mechanism Wherever your code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword: 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

Slide 4:

Java Exception Class Hierarchy 2) 3) everything else


Exceptions How do you handle exceptions? Exception handling is accomplished through the “try – catch” mechanism, or by a “throws” clause in the method declaration. Types of exception Checked exception : Checked at compile time by the complier & compels to handle the exception in the program. Unchecked exception : checked by the JVM & the compiler doesn’t compel to handle the exception .

Slide 6:

Multiple catch blocks

Slide 7:

Finally block This block contains code that is ALWAYS executed, either exception occurred or not. Syntax: try { … normal program code } catch(Exception e) { … exception handling code } finally{ }

Slide 8:

THROW This clause is used when the programmer wants to throw an exception explicitly & wants to handle it using catch block. THROWS This clause is used when the programmer does not want to handle the exception in the method & throw it out of method. Example: public void myMethod() throw Exception { try { … } catch (Exception e) { … } } public void myMethod() throws IOException {…} try { myMethod(); } catch (IOException ioe) { … } THANK YOU