• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Java Program
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Tutorial » Exception Handling In Java

Exception Handling In Java

Last Updated on: August 16, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Exception Handling In Java
  • Errors In Java Program
  • What is an exception?
  • Why an exception occurs?
  • Difference between error and exception
  • Exception Hierarchy
  • Types of exceptions
  • Checked Exception
  • Unchecked Exceptions

In the previous post, we have shared the Java programming language tutorials about Collection. in this post we are going to share another important feature of Java which allows us to handle the runtime errors and  Exception Handling In Java.

Exception Handling In Java

In this post, we are going to learn in detail about exceptions like What is Exception, Exception Hierarchy, the difference between error and exceptions, and different types of exceptions with Java program examples.

Errors In Java Program

In Java mainly there are 3 types of errors in java program:

  • Compile-time errors
  • Run time errors
  • Logical errors

Compile-time errors: When we write the program then there is a possibility of some syntactical errors due to that the program fails to compile. some examples of syntactical errors missing of the semicolon at the end of the Java statements, writing statements without proper syntax will generate a compile-time error.

package com.SoftwareTestingO.Java.ExceptionHandling;
public class Compilet_Time_Error 
{
   public static void main(String[] args) 
   {
      System.out.println("Compile Time Error Example")

   }
}

When you try to compile the java program you will get the compile-time error which state

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
   Syntax error, insert ";" to complete BlockStatements

   at com.SoftwareTestingO.Java.ExceptionHandling.Compilet_Time_Error.main(Compilet_Time_Error.java:6)
Compile Time Error Screenshot
Compile Time Error Screenshot

Run time errors: This type of error is generated when a computer system is not efficient to execute a particular statement like insufficient memory to store something or the inability of the microprocessor to execute some statements.

Example1: Runtime Error caused by dividing by zero

package com.SoftwareTestingO.Java.ExceptionHandling;

public class Run_Time_Error 
{
   public static void main(String[] args) 
   {
      int var1=10;
      int var2=0;
      
      //This statement cause the runtime error
      int var3=var1/var2;
      System.out.println("The Valued of Var3:- "+var3);
   }

}

Error Message in Console:

Exception in thread "main" java.lang.ArithmeticException: / by zero
   at com.SoftwareTestingO.Java.ExceptionHandling.Run_Time_Error.main(Run_Time_Error.java:11)

Example 2: Runtime Error caused when we are trying to Assigning/Retrieving Value from an array using an index which is greater than the size of the array

package com.SoftwareTestingO.Java.ExceptionHandling;

public class Run_Time_Error 
{
   public static void main(String[] args) 
   {
      int arr[]=new int[5];
      
      //Below statement cause the Runtime Error
      //Bcoz we are assign the value to 7 index where as our Array Max index is 5
      
      arr[7]=123;
      System.out.println("Sucessfullt Assigned the Value");
   }
}

Error Message In Console:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
   at com.SoftwareTestingO.Java.ExceptionHandling.Run_Time_Error.main(Run_Time_Error.java:12)

Logical Error: These types of errors are generated because of the flaws in the logic of the program or the program might be using a wrong formula or the design pattern of the program itself is wrong. This type of errors are not identified by the Java compiler nor the JVM, the programmer is only responsible for such type of errors.

   package com.SoftwareTestingO.Java.ExceptionHandling;

public class Logical_Error 
{
   public static void main(String[] args) 
   {
      int a=10;
      //Because of Below Statement (wrong logic) we are not getting the expected result
      if(a>=10)
      {
         System.out.println("The Value of A Is Less then 10");
      }
      else
         System.out.println("The Value of A Is Greater then 10");
   }
}

Output:

The Value of A Is Less then 10

What is an exception?

When we are executing our program at that time due to some unwanted or unexpected event the normal flow of the program interrupted. When an exception is generated when the flow of the program is terminated from that statement and we get a system generated the message. In Java we can handle the exceptions and also we can provide a meaningful message so that the user can easily understand the reason for an exception.

Why an exception occurs?

There are several reasons for occurring exceptions, but below are a few reasons which can generate an exception:

  • A user has entered an invalid data
  • File not found
  • A network connection has been lost in the middle of communications
  • The JVM has run out of a memory

So any of the reasons if an exception is occurring then we need to handle that otherwise, that may lead to a system failure. that’s why exception handling is one of the most important concepts of Java programming language.

Difference between error and exception

  • Error: It can not be handled by the program. When it happens the programmer cannot do anything.
  • Exception: An Exception is an error that can be handled. It means when an exception happens the programmer can do something to avoid any harm to the application.

Exception Hierarchy

The base or root class of exception hierarchy is Throwable, the base class is inherited by two subclasses like error and Exception. See the below image for better understand the hierarchy of exceptions.

Exception Hierarchy In Java
Exception Hierarchy In Java

Types of exceptions

There are mainly two types of exception in Java that are:

  • Checked
  • Unchecked

Checked Exception

The exceptions that are checked at compilation-time by the Java compiler are called ‘checked exceptions’. If these types of exceptions are not handled in the program then you will get a compilation error. For example, SQLException, IOException, ClassNotFoundException, etc.

Unchecked Exceptions

The exceptions that are checked by the JVM are called ‘unchecked exceptions’ or Runtime Exception. these exceptions are not checked at compile time so the JVM compiler doesn’t know the exceptions are handled or not. So its the programmer’s responsibility to handle those exceptions or provide a safe exit. Some examples of unchecked exceptions are ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

Ref: Article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers