Tips and examples:
Not recommended to catch type “Exception”, because catching the type Exception will catch all RuntimeException (like NullPointerException, ArrayIndexOutOfBoundsException) as well.
RuntimeException is unchecked exception, compile-time exception is checked exception.
Run time exceptons Example:
Not recommended to catch type “Exception”, because catching the type Exception will catch all RuntimeException (like NullPointerException, ArrayIndexOutOfBoundsException) as well.
RuntimeException is unchecked exception, compile-time exception is checked exception.
Run time exceptons Example:
Base base = new Base();
Derive d = (Derive)base; // runtime exception: java.lang.ClassCastException
deal with ClassCastException:
way 1) catch (ClassCastException e)
way 2) if (base instanceof Derive)
Derive d = (Derive)base;
Compile time exceptons Example:
FileReader throws FileNotFoundException which extends IOException
try {
BufferedReader r = new BufferedReader(new FileReader("f"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
Connection conn = dataSource.getConnection();
conn.createStatement().executeUpdate("insert into user values (null,'wjt276')");
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
No comments:
Post a Comment