Sunday, July 3, 2016

Try-Catch

Try-Catch

  1. Generalized exception catch block should be at the end in case of multiple catch block. If we declare generalized catch block at starting then it will give compile time error. Error :                  Already declared in super class
  2. We can have return statement in catch block
  3. We can have break statement in catch block if loop is present inside catch
  4. Finally block can't have return statement
  5. Finally block can have other try-catch block.
  6. Finally block can have break statement inside the loop
  7. If exception occurs in finally, execution will break/stop
  8. Try block should at least  have finally block if catch block is not provided


Nested Try Catch scenario


i. If divide by zero exception encounters in outer try block then outer try block will execute
ii. In case of inner try block exception, inner catch block will execute
iii. If divide by zero exception encounters in inner try block and if we don't have inner catch block specific for divide by zero and if we have outer catch block which specifically handles divide by zero then outer catch block will be executed.

No comments:

Post a Comment