Exception Concept In Java

Satyendra Kumar Gupta
5 min readMay 16, 2021

In this article, we will see what are Exception Handling Concept in java.

Here, First we need understand what is Exception, Advantage of Exceptions , hierarchy of Exception and Error.

Exception:

“An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of program’s instructions.”

In other words you to say abnormal condition of program knows as Exception.

So in java handle this disrupts or error using some concept(throw, throws, try, catch, finally block etc.) knows as Exception Handling.

Exception mechanism to handle the runtime errors so that normal flow of the set of program or application can be maintained.Exception are represented via class and object in java.

Advantage Of Exceptions:

1. Separating Error-Handling Code from your “Regular code”

2. Propagating Error Up the call Stack

3. Grouping and Differentiating Error Types

Hierarchy Of Exception Class:

Simple Hierarchy of Exception Class

Types Of Exceptions In Java:

1.Checked Exception

2.Unchecked Exception

3.Error

Note: When check the abnormal condition out of JVM knows as Checked Exception. And check abnormal condition in JVM knows as Unchecked Exception.

1.Checked Exception(Compile-time): It is also knows as compile-time exception. Because these exception arise at compile-time.Checked exception represent errors outside the JVM(control of the program).These Exception arise due to programmer mistakes.

For Example: throws FileNotFoundException, If the input file does not exits.

public class Class_Name{

public static void main(String args[]) throws FileNotFoundException{
File file = new File("ifFileNotExits.txt");
FileInputStream stream = new FileInputStream(file);
}
}

## This above example throws FileNotFoundException,##
*** Using try, catch block handle also this exception please see below example.***

public class Class_Name{

public static void main(String args[]) throws FileNotFoundException{
File file = new File("ifFileNotExits.txt"); //
try{
FileInputStream stream = new FileInputStream(file);
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
}

2.Unchecked Exception(Runtime): It is also knows as Runtime exception. Because these exception arise at Runtime.Unchecked program).These Exception arise when a call constructor call or method call.

For Example: ArithmeticException, NullPointerException, ArrayStoreException, etc.

public class Temp{
public void show(int n){
System.out.println("Befor:");
int x=100/n;
System.out.println(x);
System.out.println("After");
}
public static void main(String args[]){
Temp temp=new Temp();
temp.show(20);
}
}

Output:If n=20;
Before
5
After
---------------
But If n=0;
Before:
Exception in thread “main” java.lang.ArithmeticException / by zero

*** So that using try,catch black handle these exception, please see below example. ***

public class Temp{
public void show(int n){
System.out.println("Before:");
try{
int x=100/n;
System.out.println(x);
}catch(ArithmeticException e){
e.printStackTrace();
}
System.out.println("After");

}
public static void main(String args[]){
Temp temp=new Temp();
temp.show(20);
}
}

Output:
Before:
Exception in thread “main” java.lang.ArithmeticException / by zero
After

3.Error: Error is also one kind of exception. These are exceptional conditions that are external to the application. (for understanding purpose I am writing in above picture Errors comes under Unchecked Exception(Runtime)).

For Example: Suppose that an application successfully opens a file for input,but unable to read the file because of a hardware or system malfunction.That means unsuccessfully read will java.io.IOError.

Error are not subject to the Catch or Specify requirement.

Error are those exceptions indicate by Error and its sub-classes.

Some Points Related Exception:

Note 1: It can not have try catch block inside class and also class can not have inside try catch block. Please understand through below example.

class Temp{
try{
public void show(){
-------
-------
}
}catch(---){
-----
-----
}
}
###################################
try{
class Temp{
public void show(){
-------
-------
}
}
}catch(---){
-----
-----
}
*** Above Both Syntax not allowed ***

Note 2: Nested try catch Block- If try catch block inside try catch block knows as Nested Try catch Block.

For example:

try{
-----
-----
try{
-----
-----
}catch(SQLException e){
-----
-----
}
}catch(ArithmeticException e){
----
----
}
*** If you want to put try catch block inside catch block, this is passable. But these things not good for programmer. ***

Note 3: Finally Block, It is always executed, It is use paired with try and catch block(No individual finally is allowed).

More than one finally block, It is not allowed with single try or try catch Block

If it is paired with try catch block. It finally must be the last Block;

try{
-----
-----
--
}catch(----){
----
----
}
finally{
----
----
}

Note 4: In Unchecked Exception are automatically forwarded in calling chain.

In Checked Exception not possible, If exception have then Throws any Exception Like IOException.

Throw And Throws Keyword:

Throw keyword : Throw keyword is use to throw a an exception explicitly. It means create a custom error. The throw statement is used together with an exception type.

For Example:

class Temp{
int age;
public void show(int age){
if(age < 18){
ArithmeticException e = new ArithmeticException("Age should be greater then or equal to 18");
throw e;
}else{
this.age=age;
System.out.println(age);
}
}
public static void main(String args[]){
Temp temp=new Temp();
temp.show(10);
}
}
Output:In above example if you pass int value less than 18 throw keyword throw custom message like:Exception in thread “main” java.lang.ArithmeticException: Age should be greater then or equal to 18

Some point about Throw keyword:

1. It is used to create a new Exception object and throw it.

2. Using throw keyword you can declare only one Exception at a time

3. Example: throw e(please see above example) or like throw new ArithmeticException(“ here your message”);

Throws keyword : The Throws keywords is used to declare an exception. Throws is used in the signature of method to indicate that, this method might throw one of the listed type exception. These exception also handle through try-catch block.

For Example:

import java.io;
class Temp{
public static void main(String args[]) throws IOException{
FileWriter file = new FileWriter("c:\\Desktop\abc.txt");
file.write("Hello Java");
file.close();
}
}
Output: In above example if data not write in abc.txt file then give error but handle through throws keyword using IOException;

Some point about Throws Keyword:

1. It is used in method signature or definition, to declare if you already know that a risky method is being called.

2. Using throws keyword you can declare multiple Exception at a time

3. Example: throws IOException, ArrayIndexBoundException, etc.

Conclusion:

In this article, we discussed the difference between checked, unchecked and error exception. And we also discussed some keyword like try, catch, finally, throw, throws with code example. A program can use exceptions to indicate that an error occurred. And Also a program can catch exceptions by using a combination of the try , catch, finally blocks, throw and throws keyword.

Thanks.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Satyendra Kumar Gupta
Satyendra Kumar Gupta

No responses yet

Write a response