Multi -catch block is the wonderful and assert for programmer.It is introduced in java 7 as part of Exception-Handling.
Up-to Java 6 we are using multiful catch block in java programs.The main disadvantage of using this approach is increases length of the code and permanence will be decreased.To overcome this Sun people introduced Multi-catch block in Java 7 version.
Syntax for Multicatch block:
try
{
---
---
}
catch(ArithmeticException|IOException..........)
{
---
---
}
Example Program of Multicatch block:
import java.io.BufferReader;
import java.io.FileReader;
import java.io.IOException;
class Multicatch
{
public static void main(String[] args)
{
try(BufferReader br=new BufferReader(new FileReader("c:\\Data.txt")))
{
String line;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oradsn","scott","tiger");
}
catch(IOException | ClassNotFoundException | SQLException e)
{
e.printStackTrace();
}
}
}
Up-to Java 6 we are using multiful catch block in java programs.The main disadvantage of using this approach is increases length of the code and permanence will be decreased.To overcome this Sun people introduced Multi-catch block in Java 7 version.
Syntax for Multicatch block:
try
{
---
---
}
catch(ArithmeticException|IOException..........)
{
---
---
}
Example Program of Multicatch block:
import java.io.BufferReader;
import java.io.FileReader;
import java.io.IOException;
class Multicatch
{
public static void main(String[] args)
{
try(BufferReader br=new BufferReader(new FileReader("c:\\Data.txt")))
{
String line;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oradsn","scott","tiger");
}
catch(IOException | ClassNotFoundException | SQLException e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment