Google+

131. What happens when we don't handle an exception ?






Before learning how to handle the exceptions in our programs, it is useful to see what happens when we don't handle them.

Lets see what happens when we don't handle the exceptions by implementing this on Eclipse IDE ( In this example I am dividing a number by 0, say int num = 50/0, which won't show as error while writing the code but will give an error during execution i.e. Run-time ):

1.  Create a class 'ClassOne' as shown below:



2. Save and Run the 'ClassOne' file
3. Observe that the exception details are displayed in the console as shown below:



After looking at this exception in the console, its very clear that num = 50/0 statement in our code has thrown this arithmetic exception.

Also observe that the print statement (i.e. System.out.println("rest of the code"); ) after the statement num = 500/0 didn't get executed because of the exception in num = 500/0 statement which is before the print statement.

Download this project:

Click here to download the project containing the class file used in this post (You can download this project and import into Eclipse IDE on your machine)

But if we want the statement (i.e. System.out.println("rest of the code"); ) after the statement num = 500/0 to be executed irrespective of the exception in num = 500/0 statement, then we've to handle the exception in num=500 statement using Java's exceptional handling. Lets find out how to handle the exception in the next post.



Please comment below to feedback or ask questions.

 How to handle an exception will be explained in the next post.



No comments: