You are on page 1of 3

1.What is an EXCEPTION ?

Ans: Exception is a abnormal event that occurs during program execution and disrupts the normal flow of instruction. (or) An exception is a condition that is caused by a run time error in the program.when the java interpreter encounters an error such as dividing an integer by zero,it creates an exception object and throws it.If the error is not handled by the interpreter then it will display an error message. 2.How do we define TRY block ? Ans:Java uses a keyword TRY to preface a block of code that is likely to cause an error condition and throws an exception.The TRY block can hava one or more statements that could generates an exception,the remaining statement in the block are skipped and the execution jumps to the CATCH block that is palced next to the Try block. Syntax: ....... ............ ..................... try { ........... Statement; //generats an exception } Catch(Exception-type e) { Statement; //process the Exception } 3.How do we define CATCH block ? Ans:A catch block defined by the keyword Catch catches the exception thrownby the try block and handles it appropritely.The catch block is added immediately after the Try block.The catch statement takes an object of an exception class as parameter.The sope of the catch block is restricted to the statements in the precceding Try block only. Syntax: ............... try { .................. Statement;//generates an exception } Catch(Exception-type e) { Statement;//process the exception } 4.list some most common types of exceptions that might occur in java.Give examples Ans:jav has several prdefined exceptions.the most common exceptions that you may encounter are described below: Exception Type Cause of exception Arithmetic Exception: This Exception is thrown when an exceptional Arthmetic condition has occured. Ex:division by zero This Exception is thrown when an attempt is made to acess an array element beyond the index of the array. Ex:if you try to access the eleventh element Of an array that has only 10 elements.

ArrayIndexOutOfBound Exception:

ArrayStoreException:

This exception is thrown when program tries to Store wrong type of data. Ex:Storing int value insted of String. NullPointerException: This exception is thrown when an exception attempts to use null where an object is required. Ex:using an object without allocating memory for it. FileNotFoundException: This exception is thrown when we try to access non-existing file. Ex:Accessing an file that is in another directory. 6.How many Catch blocks can be use with one Try block ? Ans:It is possible to hava more than one Catch block for an Try block.When an Exception in a try block is generated,the java interpreter treates the Multiple catch blocks like cases in switch statement.The first statement whose parameter matches with the Exception object will be executed and the remaining statements are skipped. Syntax: A Try block with many Catch blocks try { Statemetn; //generates an exception } catch(Exception Type-1 e) { Statement; //process the exception of type-1 } catch(Exception Type-2 e) { Statement; //process the exception of type-2 } catch(Exception Type-3 e) { Statement; //process the exception of type-3 } 7.Create a try block that is likely to generate three types of exception and then incorporates necessary catch block to catch and handle them appropriately ? Ans: Program import java.lang.*; import java.io.*; class Except { public void static main(String args[ ]) { int a[ ]={5,10}; int b=5; int x,y,z; try { x=a[1]/a[0]-b; y=a[2]/b-a[1]; z=a[1]/c-b; } catch(ArithmeticException e) { System.out println(Division by zero); } catch(ArrayIndexOutOfBoundsException e) {

System.out println(Array index error); } catch(StoringIndexOutOfBoundsException e) { System.out println(variable not found); } int g=a[1]/a[0]; System.out.println(G=+g); } } Output: Division by zero Array index error Variable not found G=2 8.what is a Finally block ? When and how it is used? Give a suitable example. Ans:Finally block is used to handle any exception generated with in a try lock.The code in the Finally block is executed regardless of whether an exceptionis thrown or not.The finally block follows the catch block.We have only one Finally block for an excetional handler.We can use it to perform certain housekeeping operations such as closing files & releasing system resources. Syntax: try { Openfile(); Writefile(); //may cause an exception } catch(....) { Statement;//process the exception } finally { Closefile(); } 9.Explain how exception handling mechanis can be used for debugging a program ? Ans:Exception handling mechanism can be used to hide errors from rest of the program.It is possible that the programmer may misuse this technique foe hiding error rether than debugging the code.Exception handlingmay be effectively used to locate the type and place of errors.Once we identify the error,we must try to find out why these errors occur brfore we cover them up with exception handlers. 10.Define an exception calledNOMATCHEXCEPTIONthat is thrown when a string is not equal to india write a program that uses this exception. Ans:

You might also like