Sunteți pe pagina 1din 39

A Seminar On Exception Handlings in java

BY:-CHANDANI ROHDA MCA FOURTH SEM ROLLNO:-5122110008

RAIPUR INSTITUTE OF
TECHNOLOGY

Exception Handlings in java


ERRORS TYPES OF ERRORS INTRODUCTION TO EXCEPTION PURPOSE OF EXCEPTIONB HANDLING EXCEPTION TYPES COMMON JAVA EXCEPTIONS EXCEPTION HANDLING TRY BLOCK CATCH BLOCK

FINALLY BLOCK SIMPLE PROGRAM OF EXCEPTION HANDLING EXAMPLE WITH FINALLY BLOCK MULTIPLE CATCH BLOCK EXAMPLE OF MULTIPLE CATCH BLOCK SOME REMARKABLE POINTS ABOUT EXCEPTION HANDLING

ADVANTAGES OF EXCEPTION SUMMARY

ERRORS
Rarely does a program run successfully at its first attempt.a mistake might lead to an error causing to program to produce unexpected result. Errors are the wrongs that can make a program go wrong.

TYPES OF ERRORS

Compile

time error

Run time error

Compile time error


All

syntax errors will be detected and displayed by the java complier and therefore these errors are known as compile time error. Whenever the compiler displays an error ,it will not create the .class file.

Run Time Error


Sometimes a program may compile successfully creating the .class file but may not run properly.such programs may produce wrong results due to wrong logic or may terminate due to errors such as stack overflow.most common run time errors are: Dividing an integer by 0. Accessing an element that is out of bounds of array. Trying to store an element into an encompatible class or type.

Introduction to Exception
It is a condition that is caused by a run time errors in the program. A java exception is an object that describes an exceptional condition that has occurred in a piece of code.when an exceptional condition arises an object representing that exception is created and thrown in the method that caused the error.

Purpose of Exception handling


To provide means to detect and report an exceptional circumstance so that appropriate action can be taken. It performs the following tasks. Hit the exception. Throw the exception. Catch the exception. Handle the exception.

Hierarchical Structure of Exception types


Object
Throwable Error Exception

...

RuntimeException ... ...

Common java exception


ArithmeticException You are trying to use your computer to solve a mathematical problem that you cannot solve yourself. Read up on your arithmetics and try again. ArrayIndexOutOfBoundsExceptionCaused bad array index ArrayStoreExceptionYou have used all your arrays and need to buy more from the array store

FileNotFoundExceptionA file is not found IOExceptionIO stands for input/output and has to do with sending and recieving data. IO is a .security problem and should not be used.

NumberFormatExceptionYou are using outdated units of measurement, for example inches or pints. Convert to SI. There is a known bug that causes this exception to be thrown if you are very short or tall.

Exception Handling
The various keywords for handling exceptions are below. try catch finally throw throws

The try Block


The first step in constructing an exception handler is to enclose the code that might throw an exception within a try block. In general, a try block looks like the following: try { code } catch and finally blocks . . .

try{ ??? ??? } catch(<exceptionclass1> <obj1>){ ??? ??? } finally{ ??? ??? }

The catch Blocks


You associate exception handlers with a try block by providing one or more catch blocks directly after the try block. No code can be between the end of the try block and the beginning of the first catch block. try { } catch (ExceptionType name) { } catch (ExceptionType name) { }

The finally Block

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in afinally block is always a good practice, even when no exceptions are anticipated.

Simple Program
Example: The following is an array is declared with 2 elements. Then the code tries to access the 3rd element of the array which throws an exception. // File Name : ExcepTest.java import java.io.*; public class ExcepTest {

Public static void main(String args[]) { int a=10; int b=5; int c=5; int x,y; try { x=a/(b-c); //exception here }

Catch(ArithmeticException e) { System.out.println(Division by 0"); } Y=a/(b+c); System.out.println(Y=+Y); }

This would produce following result: Division by 0 Y=1

Example with Finally Block


Example: public class ExcepTest { public static void main(String args[]) { int a[] = new int[2]; try { System.out.println("Access element three :" + a[3]);

}catch(ArrayIndexOutOfBoundsException e) { System.out.println("Exception thrown :" + e); } finally { a[0] = 6; System.out.println("First element value: " +a[0]); System.out.println("The finally statement is executed");

} } } This would produce following result: Exception thrown :java.lang.ArrayIndexOutOfBoundsException: 3 First element value: 6 The finally statement is executed

Multiple catch Blocks:


A try block can be followed by multiple catch blocks. The syntax for multiple catch blocks looks like the following: try { //Protected code }catch(ExceptionType1 e1) { //Catch block }catch(ExceptionType2 e2) { //Catch block

}catch(ExceptionType3 e3) { //Catch block }

Example Of Multiple catch block

public class Multi_Catch { public static void main (String args[]) { int array[]={20,10,30}; int num1=15,num2=0; int res=0; try

{ res = num1/num2; System.out.println("The result is" +res);

for(int ct =2;ct >=0; ct--) { System.out.println("The value of array are" +array[ct]); }


}

catch (ArrayIndexOutOfBoundsException e) { System.out.println("Error?. Array is out of Bounds"); } catch (ArithmeticException e) { System.out.println ("Can't be divided by Zero"); }

} }

Output of the program: C:\Roseindia\>javac Multi_Catch.java C:\Roseindia\>java Multi_Catch Can't be divided by Zero

Throw Block
System genrated Exceptions are automatically thrown by java run time system. To manually throw the exception we use the keyword throw.

Some Remarkable Points


Note the followings: A catch clause cannot exist without a try statement. It is not compulsory to have finally clauses when ever a try/catch block is present. The try block cannot be present without either catch clause or finally clause. Any code cannot be present in between the try, catch, finally blocks.

Advantages of Exception
Separating Error-Handling Code from "Regular" Code Grouping and Differentiating Error Types

Summary

A program can use exceptions to indicate that an error occurred. To throw an exception, use the throw statement and provide it with an exception object a descendant of Throwable to provide information about the specific error that occurred. A method that throws an uncaught, checked exception must include a throws clause in its declaration.

A program can catch exceptions by using a combination of the try, catch, and finally blocks. The try block identifies a block of code in which an exception can occur. The catch block identifies a block of code, known as an exception handler, that can handle a particular type of exception.

The finally block identifies a block of code that is guaranteed to execute, and is the right place to close files, recover resources, and otherwise clean up after the code enclosed in the try block. The try statement should contain at least one catch block or a finally block and may have multiple catch blocks.

SPECIAL THANKS TO ALL

S-ar putea să vă placă și