Multiple Exceptions in Java Programs

In this post, we will discuss multiple exceptions in java programs. First of all, if the code can throw more than one kind of exception, then we can have several catch clauses with a single try to catch multiple exceptions.

Exception Hierarchy in Java.

 

Exceptions_In_Java

 

As shown in the above picture, the blue ones are checked exceptions and the red ones are unchecked exceptions.

The most specific ones should be listed first, and the more general ones later. Also, exceptions are objects from classes in java. Here, more general means a super class, and more specific means a sub class. Therefore, we have to handle the more specific exceptions first and then the generic ones. Additionally, if we reverse we might end up writing unreachable code as shown below :

Exceptions_In_Java1

 

Exception already handles the NullPointerException because it is the super class.

When an exception is thrown in a try block, the execution of the try is abandoned immediately and the system searches for a handler known as catch block in java, of the type of the exception that was thrown. Also, it’s common misconception that try block is continued after exception. However, it’s NOT the case as shown in the below picture.

Exceptions_In_Java3

 

Checked Exceptions and Unchecked Exceptions

There are two basic kinds of exceptions, Exception and RuntimeException as shown in the above picture.

Exception, is one that can occur even in a correct program. For example, when we try to create a file, the operating system may not permit it, perhaps because the disk is locked, or there is no space available. Also, these kinds of problems are beyond the control of the programmer. Therefore, even a correct program can generate them. For this reason such exceptions must be caught by the programmer. The IOException is an example. So, they must be caught.

Exceptions_In_Java2

 

In addition, a null pointer exception is a run-time exception and these need not be caught. Hence, a correct program should never have any of these, so if you have one, you need to fix your program.

Sample code with multiple catch blocks

public class MultipleExceptions {
    
    public static void main(String[] args) {
        PrintWriter writer = null;
        try
            {     
            writer = new PrintWriter(new FileWriter("?.txt"));
            } 
          catch (IOException ioe) {
                System.out.println("IOException");
            }
           catch (Exception e) {
            System.out.println("Exception");
            }
        
        finally
            {
            System.out.println("This will be executed always");
            }
        
    }

}

More on catching multiple exceptions using a single catch block in Java.

Related Posts

MultipleException_3

Java

Handle Multiple Exceptions in Java

Project From Archetype Maven

Java

Maven Project from Archetype

Java with Maven Project NetBeans

Automation, Java

Creating a Maven project using NetBeans IDE

Java Packages Test Automation

Automation, Java

Creating a Package in NetBeans IDE

Apache NetBeans Website

Automation, Java

Apache NetBeans IDE: Download and Install

Java Project NetBeans IDE

Java

Java Project using NetBeans IDE

Navigation

  • Home
  • Selenium
  • JBehave
  • Questions
  • Contact
  • Privacy
  • About

Random Posts

  • List_OperationsJava List Interface Example
  • Test webpage urlTesting webpage mobile-friendliness using a tool
  • Timeout test JUnitTesting timeouts with JUnit 4
  • JUnit Jupiter APIJUnit 5 Jupiter API Annotations
  • JUnit LibraryAdding JUnit5 library to a Project
  • Serenity BDD JBehave Result JenkinsRunning Serenity BDD stories with Jenkins
  • SerenityReportDebugDebug Serenity BDD JBehave project
  • Git Commit JBehave ProjectAdding Stories to JBehave project
  • Filtering stories JBehave MavenFiltering Stories in JBehave
  • Serenity Project Information 2Create Serenity JBehave project from command line.
  • Screenshot Test StepRunning Serenity Tests
  • No goals specified Maven ErrorHow to fix Maven error No goals specified
  • Sample_Eclipse_Check_programAutomation Environment Setup Verification
  • Blue Ocean UIBlue Ocean Jenkins Plugin
  • Android Virtual Device ManagerCreating Android Virtual Device with AVD Manager
TestingDocs.com
© 2016 - 2019 TestingDocs.com All Rights Reserved
TestingDocs.com

TestingDocs.com

Links

  • Home
  • Selenium
  • JBehave
  • Questions
  • Contact
  • Privacy
  • About

Search