The exception is an error event that can happen during the execution of a program and disrupts its normal flow. Java provides a robust and object-oriented way to handle exception scenarios, known as Java Exception Handling.
Java is an object-oriented programming language, whenever an error occurs while executing a statement, creates an exception object and then the normal flow of the program halts and JRE tries to find someone that can handle the raised exception. The exception object contains a lot of debugging information such as method hierarchy, line number where the exception occurred, type of exception etc. When the exception occurs in a method, the process of creating the exception object and handing it over to the runtime environment is called “throwing the exception”.
Table of Contents |
An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it.
Exception Hierarchy: All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class.
Firstly, the base class of all things that can be thrown is Throwable (not Exception ). Under Throwable are two subclasses: Exception and Error. Of these 4 main classes, RuntimeException and Error are unchecked (may be thrown without having to be declared as being thrown).
Handling the exception is nothing but converting system error-generated messages into user-friendly error messages. Whenever an exception occurs in the java application, JVM will create an object of appropriate exception of subclass and generates a system error message, these system-generated messages are not understandable by the user so need to convert it into a user-friendly error message. You can convert system error messages into user-friendly error messages by using the exception handling feature of java.
For Example: when you divide any number by zero the system generates / by zero so this is not understandable by the user you can convert this message into a user-friendly error message like Don't enter zero for the denominator.
The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.
The classes that extend Runtime Exception are known as unchecked exceptions e.g. Arithmetic Exception, NullPointerException, Array Index Out Of Bounds Exception etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.
There are 5 keywords used in java exception handling.
try
{
// statements cause problems at run time
}
catch(type of exception-1 object-1)
{
// statements provide a user-friendly error message
}
catch(type of exception-2 object-2)
{
// statements provide a user-friendly error message
}
finally
{
// statements that will execute compulsory
}
Example without Exception Handling
class ExceptionDemo
{
public static void main(String[] args)
{
int a=10, ans=0;
ans=a/0;
System.out.println("Denominator not be zero");
}
}
class ExceptionDemo
{
public static void main(String[] args)
{
int a=10, ans=0;
try
{
ans=a/0;
}
catch (Exception e)
{
System.out.println("Denominator not be zero");
}
}
}
Output:
Denominator not be zero
As stated earlier, when any exception is raised an exception object is getting created. Java Exceptions are hierarchical and inheritance is used to categorize different types of exceptions. Throwable is the parent class of Java Exceptions Hierarchy and it has two child objects – Error and Exception. Exceptions are further divided into checked exceptions and runtime exception.
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.