Core Java - Interview Questions and Answers for 'Catch' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Core Java - Interview Questions and Answers for 'Catch' - 9 question(s) found - Order By Rating

 Q1. What is the difference between following code segments

1.

try {
for(int i=0;i<100;i++){
calculate(i);
}
} catch (Exception ex) {
System.out.println("Error while processing")
}

and

2.

for(int i=0;i<100;i++){
try {
calculate(i);
} catch (Exception ex) {
System.out.println("Error while processing")
}
}
Core Java
Ans. In first case the whole loop will terminate as soon as any exception happens in the method calculate ( assuming calculate method is not handling its exception and those are thrown back at the calling method )

In Second case exception will be caught for individual iteration and hence it wont break the loop and will continue for the next iteration.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exception handling  try catch     Asked in 1 Companies


 Q2. How does a try statement determine which catch clause should be used to handle an exception?Core Java
Ans. When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   catch      basic        frequent


 Q3. How can we make sure that a code segment gets executed even in case of uncatched exceptions ?
Ans. By putting it within finally.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   oops   exceptions   finally   uncatched exceptions      basic        frequent


 Q4. Is it necessary that each try block to be followed by catch block ? Core Java
Ans. It should be followed by either catch or finally block.

  Sample Code for Retry in case of exception

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   catch   finally  Oracle OCA Test     Asked in 3 Companies      basic        frequent

Try 1 Question(s) Test


 Q5. Can finally block be used without catch ?Core Java
Ans. Yes but should follow "try" block then.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   catch   finally   yes-no

Try 1 Question(s) Test


 Q6. Will finally be called always if all code has been kept in try block ?Core Java
Ans. The only time finally won't be called is if you call System.exit() or if the JVM crashes first.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   try   catch   finally   system   system.exit


 Q7. Can we have try and catch blocks within finally ?Core Java
Ans. Yes, if we have a cleanup code that might throw an exception in the finally block, then we can have a try-catch block

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exceptions   exception handling   finally   try   catch   yesno


Very Frequently asked.
 Q8. Explain throw, throws , try and catch in Java ?Core Java
Ans. throw is used to re throw an exception.throws is used to declare that the method throws the respective exceptions.try block is used to identify if the respective block has thrown any exception.catch is used to catch the exception that has been thrown by the respective try block.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exception handling   throw   throws   try   catch     Asked in 5 Companies      basic        frequent


 Q9. Which of the following is not possible ?

a. try block followed by catch
b. try block followed by finally
c. try block followed by catch block and then finally
d. try block without catch or finally block
Ans. try block without catch or finally block

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     exceptions   java   try-catch   finally

Try 1 Question(s) Test



Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: