Exception handling in java

Exception in java is one of the important topics because every programmer uses this concept to control the execution of the program if anything unexcepted happens. In this post, we will understand the concept of exception handling in java and see what is the meaning of exception handling.

Here is the table content of the article will we will cover this topic.
1 What is Exception in java?
2. Exception Handling in Java?
3. Exception Hierarchy?
4. Checked Exceptions?
5. Unchecked exception?
6. How To handle Exceptions?
7. What is try and catch block?
8. What are the catch block rules?
9. What is finally block?
10. finally block with a return statement?
11. finally block with System.exit()?
12. Exception in the finally block?
13. What is throw keyword in exception handling?
14. What is throws keyword in exception handling?
15. What is the difference between throw and throws?
16. How an Exception Handled by JVM?
17. Exception handling in Method overriding?
18. Chained Exceptions in Java?
19. User-defined or Custom Exception in Java?

What is Exception in java?

During the execution of a program if any unwanted or unexpected error comes that is called an exception. It means if anything happens wrong during the execution of a program and disrupts the normal flow of the program’s instructions is called an exception. Exceptions may occur for different reasons:

  • If the user has entered the wrong data. Like user is trying to divide any value by 0 e.g. 5/0.
  • If the user tries to open a file with an invalid name.
  • If a user tries to access an array with a negative index value.
  • If a user tries to print a page but the printer doesn’t find it.

Real-life example: Let’s take an example of a courier company. If I am sending a gift to my friend but due to some reason the courier company is failed to deliver. That is called exception because this is an unexpected thing happened during normal delivery of our product. If a company handling these types of problem and send a notification to me why they were not able to deliver it on time. This handling is called exceptional handling.

Exception handling in java

In Java, if any program is disrupted by exception during execution. Then exception handling is used to handle exceptions.

Example of exception:

class FirstExampleForException
{ 
   public static void main(String args[])
   { 
      String str = null; 
      System.out.println(str.length());
   } 
}

Output : Exception in thread “main” java.lang.NullPointerException at FirstExampleForException.main(FirstExampleForException.java:4)

Explanation of example: When users try to run this program then NullPointerException will be thrown. Because str is NULL.

Exception Handling in Java

It is a mechanism to handle exceptions during the execution of the program and maintain the normal flow of execution. In other words, we can say it is a way to handle runtime errors. Java provides many keywords to handle the exceptions.

There are two types of Exceptions, Checked exception in java and unchecked exception in java. We will cover them in a separate post. Java provides some keywords to handle an exception, the try catch finally java are the keyword that is used to control the exception.

1. try keyword: The try keyword is used on the block of statements where we are excepting an exception. If any exception occurs in try block then control jumps to the next block that will be catch block or finally block. But it will handle the situation and the execution of the program will not be stopped. You can read it in detail.

2. catch keyword: The catch keyword is used with a block that is known as catch block. The catch block catches the exception those are through by try block. We can catch the exception and print the reason of the exception. You can read it in detail.

3. finally keyword: This keyword is also used with a block but it is a special block because it always executes whether any error occurs or not. We can perform some important operations like close DB connection. You can read it in detail.

public  class ExceptionHandling 
{
	public static void main(String[] args) 
    {
	   try
	   {
		   System.out.println("Before exception");
		   int a = 5/0;
		   System.out.println("After exception");
	   }
	   catch(Exception e)
	   {
		   System.out.println("From catch block");
	   }
	   finally
	   {
		   System.out.println("From finally block");
	   }
   }
}

Output: Before exception
From catch block
From finally block

Leave a Comment