Saturday, 28 April 2012

JAVA interview questions 23

Q221.What is the purpose of Exception Handling? 
Ans.The main purpose of Exception Handling is for graceful termination of the program. 

Q222.What is the meaning of Exception Handling? 
Ans. Exception Handling doesn’t mean repairing an Exception, we have to define alternative way to continue rest of the code normally. 
Example: If our programming requirement is to read the data from the file locating at London but at Runtime if London file is not available then we have to use local file alternatively to continue rest of program normally. This is nothing but Exception Handling. 

Q223.Explain Default Exception Handling Mechanism in java? 
Ans.If an exception raised, the method in which it’s raised is responsible for the creation of Exceptions object by including the following information: 
Name of the Exception 
Description of the Exception 
Stack Trace 
After creating Exception object the method handover it to the JVM. JVM checks for Exception Handling code in that method. 
If the method doesn’t contain any Exception handling code then JVM terminates the 
method abnormally and removes the corresponding entry from the stack. 
JVM identify the caller method and checks for Exception Handling code in that method. If the caller doesn’t contain any exception handling code then JVM terminates that method abnormally and removes the corresponding entry from the stack. 
This process will be continue until main() method. 
If the main() method also doesn’t contain exception handling code the JVM terminates that main() method and removes the corresponding entry from the stack. Just before terminating the program abnormally JVM handovers the responsibility of 
exception handling to the Default Exception Handler which is the component of JVM. 
Default Exception Handler just print exception information to the consol in the following format 

Name of Exception: Description 
Stack Trace (Location of the Exception) 

Q224.What is the purpose of try? 
Ans We should maintain all risky code inside the try block. 


Q225. What is the purpose of catch block? 
Ans.We have to maintain all Exception Handling code inside the catch block. 

Q226. Is try with multiple catch block is possible? 
Ans. The way of handling an exception is varied from exception to exception compulsory we have to write a separate catch block for every exception. Hence try will multiple catch block is possible and it is recommended to use. 
Example: 
try{ 
//Risky code
}
catch(IOException e)
{
//Hndling code for IOException
}
catch(ArithmeticException e) { 
//handling code for AE
}
catch(NullPointerExcetpion e)
{
// handling code for NPE
}
catch(Exception e)
{
//default exception handling code
}


Q227. If try with multiple catch block present is order of catch blocks important in which order we have to take? 
Ans. If try with multiple catch block present then the order of catch block is very important it should be from child to parent but not from parent to child. 


Q228. What are various methods to print Exception information? and differentiate them. 
Ans. 
Throwable class defines the following method to print exception or error information . 
1. printStackTrace() :- This method print exception information in the following format. 
Name of the Exception: Description 
StackTrace 
2.toString():- This method print exception information in the following format. 
Name of the Exception: Description 
3.getMessage():- This method prints only description of the exception. 
Description 


Q229.If an exception rised inside catch block then what will happen? 
Ans. If an exception raised inside catch block and it is not part of any try block then it is always abnormal termination. 

Q230. Is it possible to take try, catch inside try block? 
Ans. Yes, It is possible to take try, catch inside try block. That is nesting of try catch is possible.

No comments:

Post a Comment