Core Java - Interview Questions and Answers for 'Try catch' | Search Java 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 'Try catch' - 1 question(s) found - Order By Newest

 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 Java2016-12-29 15:42:10

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


Related Questions

  What are the ways to avoid LazyInitializationException ?
  What things should be kept in mind while creating your own exceptions in Java?
  Explain java.lang.OutOfMemoryError ?
  When do you get ClassCastException?

or

What is ClassCastException ?
  How compiler handles the exceptions in overriding ?
  If you are given a choice to implement the code to either Insert a Record or Update if already exist, Which approach will you follow ?
  Explain throw, throws , try and catch in Java ?
 What will happen if we don't have termination statement in recursion ?
  Name few Java Exceptions ?
  How can we make sure that a code segment gets executed even in case of uncatched exceptions ?



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: