SQLException vs RuntimeException Runtime Exception vs Checked Exception in Java

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<Throwable>

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值