Exception handling

  • Eclipse: Oxygen
  • Java: 1.8

Exception Handling is the done to avoid errors which occur during the runtime of the program. Hence in such cases, we take care of such exceptions to prevent the program from ending abruptly. A number of things can give rise to the following kinds of exceptions such as the file not found an error or any type of input type error where the datatype was different than expected or etc. We learn to avoid this and structure our program accordingly in this section.

App class

Output

The value of y is 0

Exception Handling, Multiple Catch Blocks

It shows multiple catch blocks for a single try block. It also shows the hierarchy of the exception classes. All the exceptions can be further classified into Throwable and Unchecked Exception which can be further classified into many more which will be seen forward.

App.Java

Output

The value of y is 0

This further explains is the concept of a hierarchy of the exception classes. This hierarchy if not maintained can transform into the code being unreachable at some points. For example, if the Exception catch block appears before the Arithmetic Exception Block then the latter will become unreachable as the Exception will catch all the thrown exceptions.

App.Java

Output

Arithmetic Exception occurred

Contributed by: Poonam Tomar


VIEW COURSES

Buy me a coffee!

 

PayTM QR (INDIA)

  • Get video tutorials
  • Project files set
  • Get┬áCertified

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu