Java Exception – Explore Checked & Unchecked Exception With Examples
Java exception arises during the execution of a program. Exceptions occur unexpectedly and can result in disrupting the normal flow of the program. Java provides a robust way to handle exceptions known as Exception Handling.
This is something that every programmer faces at any point of coding. They can occur from different kind of situations like entering the wrong data, hardware failure, network connection failure, Database server down, etc.
Have you checked the current Career Opportunities in Java?
Following are a few situations where Java exception occurs.
- A user has entered invalid information.
- File not found is also an exception.
- Connection lost in the middle of the communication
- Shortage of memory
- Hardware failure.
Some of these Java exceptions are caused by user’s mistake, others by developer blunder, and others by physical assets that have flopped in some way.
Stay updated with latest technology trends
Join DataFlair on Telegram!!
Types of Java Exceptions
1. Checked Exception in Java
Checked Exception is also known as compile-time exceptions because it occurs at the compile time.
If there is some code within a method which throws a checked exception, we must catch it in the same method or we have to propagate it to the caller using throws keyword.
For example IOException, SQLException, etc.
Don’t forget to check – DIfference Between Checked and Unchecked Exceptions in Java
2. Unchecked Exception in Java
Unchecked Exception is also known as Runtime Exceptions because it occurs at Runtime. This kind of exception occurs at the time of execution. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It totally depends upon the programmer to catch the exceptions.
For example ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.
3. Java Error
Error is not an exception by any means, but rather issues that emerge outside the ability to control the user or the developer. Errors in Java are normally overlooked in your code since you can seldom take care of a mistake. For instance, if a stack flood happens, a blunder will emerge. They are additionally overlooked at the season of aggregation
For example VirtualMachineError, AssertionError etc.
Exception Hierarchy in Java
All exception classes are subtypes of the java.lang.Exception class. The special case class is a subclass of the Throwable class. Other than the special case class we also have another subclass called Error which we get from the Throwable class.
Recommended Reading – Inner Class in Java with Example!
Java Errors are irregular conditions that occur if there should an occurrence of extreme disappointments, these are not taken care of by the Java programs. Errors are created to demonstrate blunders produced by the runtime condition. Case: JVM is out of memory. Ordinarily, programs can’t recover from mistakes.
Every problem has a solution, you just have to be creative enough to find it.
There are three types of exceptions we studied in Java, checked, unchecked and error. Every exception has its own way to handle it. Don’t worry, in our next article, we will discuss how to handle these exceptions in Java with the example, which will solve all your queries.
How was the explanation? Please give us your valuable feedback, it will help us to serve you better!