Saturday, May 4, 2024
HomeJavaThe best way to use A number of Catch block for Exception...

The best way to use A number of Catch block for Exception dealing with in Java? Instance Tutorial


Disclosure: This text might include affiliate hyperlinks. While you buy, we might earn a fee.

Java 7 in some ways improved exception dealing with. Two of the function of Java 7 which improves exception dealing with are the flexibility to catch the a number of exceptions in a single catch block and shutting assets routinely utilizing Automated useful resource administration block. Java has lengthy been criticized for its verbose exception dealing with code, obligatory to deal with checked Exceptions in Java. Programmers all the time complained that it clutters the code and lowered readability. Java 7 someway reduces this ache by bettering the Exception dealing with function e.g. a number of catches and ARM blocks. On this Java 7 tutorial, we’ll see tips on how to catch a number of exceptions in a single catch block utilizing JDK7.

The best way to catch a number of Exceptions in a single catch block

How to catch multiple exception in one block - Java 7 exampleOne place the place I really feel catching a number of exceptions in a single catch block helps is Java database connectivity, Java IO, and formatting date in Java. Since most of JDBC code throws checked SQLException, IO-related code throws IOException, and date formatting throws ParseException, which might’t be dealt with in a single catch block previous to Java 7.

Although there may be an alternative choice to catch java.lang.Exception as an alternative of java.io.IOException and subsequently checked exceptions however that strategy is fragile as a result of it’ll additionally catch unintended exceptions.

Catching java.lang.Exception means your code is open for all form of exceptions, it is not thought-about pretty much as good apply. With a number of exception catch block in Java 7, you possibly can mix dealing with of those two exceptions at one place within the code.

Code earlier than Java 7

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.textual content.ParseException;
import java.textual content.SimpleDateFormat;
import java.util.Date;

/**
 *
 * Java program to catch a number of exception earlier than Java 7
 */

public class BeforeJava7{

    public static void foremost(String args[]) {
        attempt {
            File file = new File(“take a look at.txt”);
            FileInputStream fis = new FileInputStream(file);
            BufferedReader bReader = new BufferedReader(new InputStreamReader(fis));
            String str = bReader.readLine();
            SimpleDateFormat format = new SimpleDateFormat(“DD/MM/YY”);
            Date date = format.parse(str);
         
        } catch (ParseException exception) {
            //code to deal with ParseException
        } catch (IOException exception) {
            //code to deal with IOException
        }
    }
}

After Java 7, each of catch block will be changed with a easy a number of exception catch block, as proven under:

attempt{
   //code
}catch(ParseException | IOException ex) {
   // deal with ParseException and IOException right here.
}

Listed below are some extra vital options from Java 7 launch, you possibly can examine them right here:

improved exception handling of Java 7
The vital level to notice is that variable ex is the ultimate variable right here and cannot be reassigned. One other place the place you need to use this improved Exception dealing with function is to mix two catch blocks which are basically doing the identical job or only a copy-paste. Through the use of Java 7 a number of catch block, you possibly can cut back lots of boilerplate code in Java.

Different Java 7 tutorials for Java programmer

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments