Top 10 Questions about Java Exceptions--reference
reference from:http://www.programcreek.com/2013/10/top-10-questions-about-java-exceptions/
This article summarizes the top 10 frequently asked questions about Java exceptions.
1. Checked vs. Unchecked
In brief, checked exceptions must be explicitly caught in a method or declared in the method's throws clause. Unchecked exceptions are caused by problems that can not be solved, such as dividing by zero, null pointer, etc. Checked exceptions are especially important because you expect other developers who use your API to know how to handle the exceptions.
For example, IOException is a commonly used checked exception and RuntimeException is an unchecked exception. You can check out the Java Exception Hierarchy Diagram before reading the rest.
2. Best practice for exception management
If an exception can be properly handled then it should be caught, otherwise, it should be thrown.
3. Why variables defined in try can not be used in catch or finally?
In the following code, the string s declared in try block can not be used in catch clause. The code does not pass compilation.
try { |
The reason is that you don't know where in the try block the exception would be thrown. It is quite possible that the exception is thrown before the object is declared. This is true for this particular example.
4. Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions?
They actually throw different exceptions. This is a problem of JDK. They are developed by different developers, so it does not worth too much thinking.
Integer.parseInt(null); |
5. Commonly used runtime exceptions in Java
Here are just some of them.IllegalArgumentException
ArrayIndexOutOfBoundsException
They can be used in if statement when the condition is not satisfied as follows:
if (obj == null) { |
6. Can we catch multiple exceptions in the same catch clause?
The answer is YES. As long as those exception classes can trace back to the same super class in the class inheritance hierarchy, you can use that super class only.
7. Can constructor throw exceptions in java?
The answer is YES. Constructor is a special kind of method. Here is a code example.
8. Throw exception in final clause
It is legal to do the following:
public static void main(String[] args) { |
But to have better code readability, you should wrap the embedded try-catch block as a new method, and then put the method invocation in the finally clause.
public static void main(String[] args) { |
9. Can return be used in finally block
Yes, it can.
10. Why developers consume exception silently?
There are so many time code segments like the following occur. If properly handling exceptions are so important, why developers keep doing that?
try { |
Ignoring is just easy. Frequent occurrence does not mean correctness.
Top 10 Questions about Java Exceptions--reference的更多相关文章
- Top 10 questions about Java Collections--reference
reference from:http://www.programcreek.com/2013/09/top-10-questions-for-java-collections/ The follow ...
- Top 10 Methods for Java Arrays
作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The ...
- 【翻译】Java Array的排名前十方法(Top 10 Methods for Java Arrays)
这里列举了Java Array 的前十的方法.他们在stackoverflow最大投票的问题. The following are top 10 methods for Java Array. The ...
- Top 10 Mistakes Java Developers Make--reference
This list summarizes the top 10 mistakes that Java developers frequently make. #1. Convert Array to ...
- Top 10 Mistakes Java Developers Make(转)
文章列出了Java开发者最常犯的是个错误. 1.将数组转换为ArrayList 为了将数组转换为ArrayList,开发者经常会这样做: ? 1 List<String> list = A ...
- Yet Another 10 Common Mistakes Java Developers Make When Writing SQL (You Won’t BELIEVE the Last One)--reference
(Sorry for that click-bait heading. Couldn’t resist ;-) ) We’re on a mission. To teach you SQL. But ...
- Top 10 Algorithms for Coding Interview--reference
By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...
- 转:Top 10 Algorithms for Coding Interview
The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...
- Favorites of top 10 rules for success
Dec. 31, 2015 Stayed up to last minute of 2015, 12:00am, watching a few of videos about top 10 rules ...
随机推荐
- 基于CommentCoreLibrary简单的弹幕实现
本文地址:http://www.cnblogs.com/liaoyu/p/ccl-demo.html 实现基于开源的 CommentCoreLibrary 最近有需求要实现一个简单的评论弹幕实现,通过 ...
- 判断微信内置浏览器的UserAgent
要区分用户是通过"微信内置浏览器"还是"原生浏览器"打开的WebApp, 可以通过navigator.userAgent来进行判断. 以下是对各种平台上微信内置 ...
- plsql exist和in 的区别
<![endif]--> <![endif]--> 发现公司同事很喜欢用exists 和in 做子查询关联,我觉得很有必要研究下 两者的区别,供参考和备忘 /* (这段信息来自 ...
- Android 完全退出程序,以及再按一次返回键退出程序
再按一次返回键退出最终完整方案: boolean isExit; @Override protected void onCreate(Bundle savedInstanceState) { ...
- INCOIN Importing Multilingual Items (Doc ID 278126.1)
APPLIES TO: Oracle Inventory Management - Version: 11.5.9 to 11.5.10.CU2 - Release: 11.5 to 11.5 GOA ...
- POJ1699Best Sequence(DFS)
链接 这题其实是由bug的 一个串包含其它两个串的数据没有 所以就这么水了它吧 只处理两个串的关系就行了 回来补点..看了huge的博客 发现其实不是有Bug 题意没读清楚 必须首尾相连 像AGCT ...
- Understanding Memory Management(2)
Understanding Memory Management Memory management is the process of allocating new objects and remov ...
- win7 64位系统 pl/sql 无法解析指定的连接标识符解决办法
我用的是win764位,装好后,装了pl/sql 和toad,都连不上数据库,报错位“无法解析指定的连接标识符” 解决办法,经过研究发现安装目录有问题.默认会安装在“C:\Program Files ...
- 1.进入debug模式(基础知识列表)
1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...
- hibernate之参数绑定
hibernate之参数绑定 ---------- 我们应该拒绝SQL(或HQL)的拼装,应该永远不要编写这样的代码,有这很严重的安全问题,众所周知的SQL注入.我们可以考虑参数绑定,在hiberna ...