Exception Handling in Java

Views:
 
Category: Education
     
 

Presentation Description

Exception Handling

Comments

Presentation Transcript

Presentation On Exception Handling in Java:

Presentation On Exception Handling in Java Presented by : Sandeep Kumar Soamya Malviya MCA (4 th SEM)

PowerPoint Presentation:

An exception is a problem that arises during the execution of a program. An exception can occur for many different reasons, including the following: 1 A user has entered invalid data. 2 A file that needs to be opened cannot be found. 3 A network connection has been lost in the middle of communications, or the JVM has run out of memory. 4 Some of these exceptions are caused by user error , others by programmer error , and others by physical resources that have failed in some manner.

PowerPoint Presentation:

To understand how exception handling works in Java, we must need to understand the three categories of exceptions: Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

PowerPoint Presentation:

Runtime exceptions: A runtime exception is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation. Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.

PowerPoint Presentation:

Program which throws Exception : class abc { public static void main(String args []) { System.out.println (“3/0”); System.out.println (“Pls. print me.”); } }

PowerPoint Presentation:

What Happens When an Exception Occurs? When an exception occurs within a method, the method creates an exception object and hands it off to the runtime system Creating an exception object and handing it to the runtime system is called “throwing an exception” Exception object contains information about the error, including its type and the state of the program when the error occurred

PowerPoint Presentation:

How to handle exceptions? Exception handling is accomplished through the “try – catch” mechanism, or by a “throws” clause in the method declaration. For any code that throws a checked exception, we can decide to handle the exception our self, or pass the exception “up the chain” (to a parent class). Contd …..

PowerPoint Presentation:

Contd …… To handle the exception, we write a “try-catch” block. To pass the exception “up the chain”, we declare a throws clause in our method or class declaration. If the method contains code that may cause a checked exception, we must handle the exception OR pass the exception to the parent class (remember, every class has Object as the ultimate parent)

PowerPoint Presentation:

Try-Catch Mechanism Wherever our code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword. After the try block, the code to handle the exception should it arise is placed in a block of code starting with the “catch” keyword . We may also write an optional “finally” block. This block contains code that is ALWAYS executed, either after the “try” block code, or after the “catch” block code. Finally blocks can be used for operations that must happen no matter what (i.e. cleanup operations such as closing a file)

PowerPoint Presentation:

Example try { … normal program code } catch(Exception e ) { … exception handling code }

PowerPoint Presentation:

Passing the exception In any method that might throw an exception, we may declare the method as “throws” that exception, and thus avoid handling the exception ourself . Example public void myMethod throws IOException { … normal code with some I/O }

Thanks:

Thanks