• TestingDocs
TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Exceptions in Java Programs

Overview

In this post, we will discuss 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

 

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");
            }
        
    }

}

 

Handle Multiple Exceptions

https://www.testingdocs.com/handle-multiple-exceptions-in-java/

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Java Performance

Java Tutorials /

Improving Java Performance with Multithreading

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

‹ Java List Interface Example› Handle Multiple Exceptions in Java

Recent Posts

  • How to secure your SQL Database: Tips and Tricks
  • Shaping the Future of Development: Exploring Key Trends in Software Engineering
  • Improving Java Performance with Multithreading
  • Difference between PHP and JavaScript?
  • Bing Conversation Styles
  • ChatGPT Introduction
  • Open Source AI Frameworks
  • Artificial Intelligence Tools
  • Top AI Music Applications
  • Top AI Website Design Tools

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com