Switch to full style
Java Technology Tutorials Written By Members.
Post a reply

Multiple Exception Catching

Mon Nov 05, 2012 9:35 pm

In java7 you can do multiple exception catching instead of adding many catches , for example imagine we have the following snippet in java6 :
java code
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ReadMyFile {

    public static void main(String args[]) {

        Connection oracleConnection = null;
        String filePath = "C://file.txt";
        try {
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read();
            oracleConnection = DriverManager.getConnection("dbURL", "dbUsername", "dbPass");
        } catch (SQLException ex) {
            Logger.getLogger(ReadMyFile.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
 


At java7 it will be Multiple Exception Catching like this:
java code
      try {
            File file = new File(filePath);
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read();
            oracleConnection = DriverManager.getConnection("dbURL", "dbUsername", "dbPass");
        } catch (SQLException | IOException ex) {
            ex.printStackTrace();
        }

 




Post a reply
  Related Posts  to : Multiple Exception Catching
 Multiple Inheritance     -  
 Jtable with multiple headings     -  
 Using Multiple Values for a Cookie in php     -  
 calculate data from multiple selection     -  
 Code to open multiple links.     -  
 send mail to Multiple recipients     -  
 Out of Bounds exception     -  
 Exception handling     -  
 Exception is not clear     -  
 Javascript Validation On Multiple Select Lists     -  

Topic Tags

Java Exceptions