Exception handling is one of the most important feature of java programming that allows us to handle the runtime errors caused by exceptions. In this guide, we will learn what is an exception, types of it, exception classes and how to handle exceptions in java with examples.
What is an exception?
An Exception is an unwanted event that interrupts the normal flow of the program. When an exception occurs program execution gets terminated. In such cases we get a system generated error message. The good thing about exceptions is that they can be handled in Java. By handling the exceptions we can provide a meaningful message to the user about the issue rather than a system generated message, which may not be understandable to a user.
Why an exception occurs?
There can be several reasons that can cause a program to throw exception. For example: Opening a non-existing file in your program, Network connection problem, bad input data provided by user etc.
Exception Handling
If an exception occurs, which has not been handled by programmer then program execution gets terminated and a system generated error message is shown to the user. For example look at the system generated exception below:
An exception generated by the system is given below
Exception in thread "main" java.lang.ArithmeticException: / by zero at ExceptionDemo.main(ExceptionDemo.java:5) ExceptionDemo : The class name main : The method name ExceptionDemo.java : The filename java:5 : Line number
This message is not user friendly so a user will not be able to understand what went wrong. In order to let them know the reason in simple language, we handle exceptions. We handle such conditions and then prints a user friendly warning message to user, which lets them correct the error as most of the time exception occurs due to bad data provided by user.
Advantage of exception handling
Exception handling ensures that the flow of the program doesn’t break when an exception occurs. For example, if a program has bunch of statements and an exception occurs mid way after executing certain statements then the statements after the exception will not execute and the program will terminate abruptly.
By handling we make sure that all the statements execute and the flow of program doesn’t break.
Difference between error and exception
Errors indicate that something severe enough has gone wrong, the application should crash rather than try to handle the error.
Exceptions are events that occurs in the code. A programmer can handle such conditions and take necessary corrective actions. Few examples:
NullPointerException – When you try to use a reference that points to null.
ArithmeticException – When bad data is provided by user, for example, when you try to divide a number by zero this exception occurs because dividing a number by zero is undefined.
ArrayIndexOutOfBoundsException – When you try to access the elements of an array out of its bounds, for example array size is 5 (which means it has five elements) and you are trying to access the 10th element.
Types of exceptions
There are two types of exceptions in Java:
1)Checked exceptions
2)Unchecked exceptions
I have covered this in detail in a separate tutorial: Checked and Unchecked exceptions in Java.
Checked exceptions
All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled/declared in the program, you will get compilation error. For example, SQLException, IOException, ClassNotFoundException etc.
Unchecked Exceptions
Runtime Exceptions are also known as Unchecked Exceptions. These exceptions are not checked at compile-time so compiler does not check whether the programmer has handled them or not but it’s the responsibility of the programmer to handle these exceptions and provide a safe exit. For example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
Compiler will never force you to catch such exception or force you to declare it in the method using throws keyword.
sravani says
hi Chaitanya Singh,
thanks for your efforts it is really appreciated.this is the best site i have ever seen. very clear explanation. please upload the frameworks also.then it will be very helpful to us.
Poornima says
Explanation is good… Thanks
akash says
good reply
satyabrata barik says
Hi Chaitanya,
Thanks a lot for putting such hard work for us and I can say this is the most simplest website where the beginners really will be start focusing. It helped me and I usually go through your others subject when i get some time.
sampath says
Am Lecturer in Computer Science, This site is an Excellent site. Very Good Explanation and easy understandable examples. very good. update frequently.
kumar vishal says
Explanation is good.But, try to give more examples. Examples are so easy. For programming examples should be more for practices.
Harsh says
Please tell me how to handle checked and unchecked exception with example?
Ashfaq says
Same mechanism is used to handle any exception. Difference between checked and unchecked exception is that if you don’t handle checked exception using try…catch block in your program then it will not compile.
Vikram says
Hi,
THis is very good site having explanation of core concepts in very simple manner.
Regards
Vikram
chandrasekhar says
Hi Chaitanya,
beginnersbook helped me a lot.Thank you.
Abhinav Kumar says
Hi Chaitanya,
Beginners Book is really one of the best to the ground sites for the basic JAVA learners.
Thanks a lot for it…!!
hajra says
thank you so much for a simple and clear explanation…
Nawal Sah says
What is the difference between compiler error and exception?
praveen says
Compiler error:when we compile the program then we will get compile time error.
Exception:an unwanted unexpected event disturbs the normal flow of the program execution.
Nawal Sah says
What code block should we never return from and why?
Nawal Sah says
What is Error? Can we catch Errors? If yes, what should be done in the catch block and why?
Chitra says
thanks a lot.. it is nice blog to know the java concepts.. simple and easy yar… :)
Brian Wekesa says
good work I enjoyed reading it……….#bryan
Sandor Szekeres says
“When an exception occurs program processing gets terminated and doesn’t continue further.”
It is not strictly true… If we handle the exception correctly then it can continue processing.
Am I right?
See the first exception of this article:
https://beginnersbook.com/2013/04/try-catch-in-java/
Deepan says
The way you have explained is really awesome, also you have provided the examples where ever required.
thanks for your great effort.
Gajanan Kharat says
Hello Chaitanya,
Nice explanation about Exception handling but Could you please elaborate How ClassNotFoundException
comes under the “Checked Exception”, I guess it should be unchecked exception