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

    Handle Multiple Exceptions in Java

    Java

    Overview

    In this section, we will learn to handle multiple exceptions in java automation code. We can use multiple catch blocks to handle multiple exceptions in the code.

    Sample Code

    import java.io.IOException;
    
    public class MultipleExceptions {
    
    public static void main(String[] args) {
    try {
    throw new Exception("Throwing Sample Exception");
    }
    catch(IOException ioe) {
    ioe.printStackTrace();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    
    }
    
    }

     

    Common mistake

    While using multiple catch statements in automation code, it is important to remember that sub class of class Exception inside catch must come before any of their super class. IOException is a sub-class of Exception class in the class hierarchy.

    import java.io.IOException;
    
    public class MultipleExceptions {
    
    public static void main(String[] args) {
    try {
    throw new Exception();
    }
    catch(Exception e) {
    e.printStackTrace();
    }
    catch(IOException ioe) {
    ioe.printStackTrace();
    
    }
    }
    
    }

     

    The above code would cause a compile-time error.

    Compile time error:

    Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    Unreachable catch block for IOException. It is already handled by the catch block for Exception

    at MultipleExceptions.main(MultipleExceptions.java:12)

    Handling in a single block

    Catching two or more exceptions in a single handler catch block. The following shows the usage of handling two exceptions with a single catch block.

    public class MultipleExceptions {
    
    public static void main(String[] args) {
    try {
    int[] a= {1,2,3};
    double b= a[1]/a[2];
    System.out.println(b);
    }
    catch(IndexOutOfBoundsException | ArithmeticException e) {
    e.printStackTrace();
    }
    
    }
    
    }

     

    —

    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

    Exceptions_In_Java

    Java Tutorials /

    Exceptions in Java Programs

    ‹ Exceptions in Java Programs› Java Testing Tools

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com