Exception Handling in Java with examples

In the previous tutorial, we learned about the Array in Java. In this tutorial, we will learn about Exception Handling in Java with examples.


Exception Handling in Java: Exceptions in java are unexpected events and conditions that happen during runtime. We will understand more about these exceptional situations with the help of an example –

We will consider an example of the program of an ATM Machine. We can do many functions such as withdrawing our balance, checking our balance, etc. But, there may occur exceptional situations like insufficient balance, or the amount user entered cannot be withdrawn because it is not a multiple of 100. Or, the amount user entered is more than the daily limit.

Exception Handling in Java with examples er.yuvayana.org
Exception Handling in Java with examples er.yuvayana.org

So, to take care of such situations, we should make our program robust. We need to write some pieces of code, that will take care of when such exceptional situations exist.

This is called exception handling in Java.

What happens when Exceptions occur in Java?

Whenever any exceptional situation occurs, that Java considers to be exceptional, the program stops executing. In this case, program execution stops displaying a message on the console screen. This is called a runtime error message.

There are many such situations existing in Java. For example, if we try to divide any number by zero, this is also a runtime exception.

The problem in this is that the execution of the program stops and the line written after that will not run. This is the problem that we have to deal with.

Exception Handling in Java

When an error occurs in some piece of code, the code jumps to the place where the catch mechanism for it is written.

Writing the catch mechanism is necessary for situations that do not pre-exist in Java. But for some situations, a default catch mechanism is already written in Java.

But, the default catch mechanism does not make our program Robust. Because of the following two reasons –

  • The error message printed is in a technical language that is understood by only a programmer. Not, by any common user.
  • Second, the program is ended after printing the error on the console screen and further lines cannot be executed.

So, we need to write our mechanism that will run when exceptions come, and we also need to write the throw keyword where the exceptional codes are written. So, there are 4 combinations possible for this –

  • Default Throw and Default Catch
  • Default Throw and Our Catch
  • Our Throw and Default Catch
  • Our Throw and Our Catch

In the above 4 possibilities, the program will not end the case if our catch mechanism is run

Throwable Class

  • The throwable class provides a String variable in java that can be set by the subclasses to provide a detailed message that provides more information about the exception that occurred.
  • All classes of Throwables define a one-parameter constructor that takes a string as the detail message.
  • The class Throwable provides a getMessage() function to retrieve an exception

In this article, we had a brief knowledge of Exceptional Handling. But, we will learn about the practical implementation of these in the upcoming articles.

Related Quiz:

Previous articleWhat is Array in Java ?
Next articleBusiness Consultancy Solution – Busting a scheme
Hello Guys, This website is maintained by the Yuvayana Writing Staff. We are trying to give you new and useful articles on daily basis. You can help us by sharing these articles with your friends on social networking websites. Please also provide your feedback via comment of each and every test that you have attempted.


Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.