您当前的位置: 首页 >  Java

mutourend

暂无认证

  • 1浏览

    0关注

    661博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

SQLException vs RuntimeException Runtime Exception vs Checked Exception in Java

mutourend 发布时间:2019-06-27 14:20:13 ,浏览量:1

1. SQLException vs RuntimeException
public class RuntimeException
extends Exception

RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
RuntimeException and its subclasses are unchecked exceptions. 
Unchecked exceptions do not need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.
public class SQLException
extends Exception
implements Iterable

An exception that provides information on a database access error or other errors.
Each SQLException provides several kinds of information:
1) a string describing the error. This is used as the Java Exception message, available via the method getMesasge.
2) a "SQLstate" string, which follows either the XOPEN SQLstate conventions or the SQL:2003 conventions. The values of the SQLState string are described in the appropriate spec. The DatabaseMetaData method getSQLStateType can be used to discover whether the driver returns the XOPEN type or the SQL:2003 type.
3) an integer error code that is specific to each vendor. Normally this will be the actual error code returned by the underlying database.
4) a chain to a next Exception. This can be used to provide additional error information.
5) the causal relationship, if any for this SQLException.

An SQLException can occur both in the driver and the database. When such an exception occurs, an object of type SQLException will be passed to the catch clause.

try {
   // Your risky code goes between these curly braces!!!
}
catch(Exception ex) {
   // Your exception handling code goes between these 
   // curly braces, similar to the exception clause 
   // in a PL/SQL block.
}
finally {
   // Your must-always-be-executed code goes between these 
   // curly braces. Like closing database connection.
}

Unchecked means compiler doesn’t check and Checked means compiler checks for exception handling.

2. Runtime Exception vs Checked Exception in Java

Java Exceptions are divided in two categories RuntimeException also known as unchecked Exception and checked Exception. Main difference between RuntimeException and checked Exception is that, It is mandatory to provide try catch or try finally block to handle checked Exception and failure to do so will result in compile time error, while in case of RuntimeException this is not mandatory. Difference between checked and unchecked exception is one of the most popular question on Java interview for 2 to years experienced developer especially related to Exception concepts. Answer to this question is rather similar as mentioned in previous lines and they are mostly asked along with other Java Exception interview questions like difference between throw and throws and Error vs Exception. Any Exception which is subclass ofRuntimeException are called unchecked and mandatory exception handling is not requirement for them.

Some of the most common Exception like NullPointerException, ArrayIndexOutOfBoundException are unchecked and they are descended from java.lang.RuntimeException. Popular example of checked Exceptions are ClassNotFoundException andIOException and that’s the reason you need to provide a try catch finally block while performing file operations in Java as many of them throws IOException.

Similarly many utilities of Reflection API throws java.lang.ClassNotFoundException. In this Java tutorial we will see some more difference between RuntimeException and checked Exception in Java.

参考资料: [1] https://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html [2] https://docs.oracle.com/javase/7/docs/api/java/sql/SQLException.html [3] https://www.tutorialspoint.com/jdbc/jdbc-exceptions.htm [4] https://www.java67.com/2012/12/difference-between-runtimeexception-and-checked-exception.html

关注
打赏
1664532908
查看更多评论
立即登录/注册

微信扫码登录

0.0366s