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 {
File file = new File("path");
FileInputStream fis = new FileInputStream(file);
String s = "inside";
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println(s);
}

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);
// throws java.lang.NumberFormatException: null
 
Double.parseDouble(null);
// throws java.lang.NullPointerException

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) {
throw new IllegalArgumentException("obj can not be 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) {
File file1 = new File("path1");
File file2 = new File("path2");
try {
 
FileInputStream fis = new FileInputStream(file1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
FileInputStream fis = new FileInputStream(file2);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}

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) {
File file1 = new File("path1");
File file2 = new File("path2");
try {
 
FileInputStream fis = new FileInputStream(file1);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
methodThrowException();
}
}

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 {
...
} catch(Exception e) {
e.printStackTrace();
}

Ignoring is just easy. Frequent occurrence does not mean correctness.

Top 10 Questions about Java Exceptions--reference的更多相关文章

  1. Top 10 questions about Java Collections--reference

    reference from:http://www.programcreek.com/2013/09/top-10-questions-for-java-collections/ The follow ...

  2. Top 10 Methods for Java Arrays

    作者:X Wang 出处:http://www.programcreek.com/2013/09/top-10-methods-for-java-arrays/ 转载文章,转载请注明作者和出处 The ...

  3. 【翻译】Java Array的排名前十方法(Top 10 Methods for Java Arrays)

    这里列举了Java Array 的前十的方法.他们在stackoverflow最大投票的问题. The following are top 10 methods for Java Array. The ...

  4. Top 10 Mistakes Java Developers Make--reference

    This list summarizes the top 10 mistakes that Java developers frequently make. #1. Convert Array to ...

  5. Top 10 Mistakes Java Developers Make(转)

    文章列出了Java开发者最常犯的是个错误. 1.将数组转换为ArrayList 为了将数组转换为ArrayList,开发者经常会这样做: ? 1 List<String> list = A ...

  6. 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 ...

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

  8. 转:Top 10 Algorithms for Coding Interview

    The following are top 10 algorithms related concepts in coding interview. I will try to illustrate t ...

  9. 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 ...

随机推荐

  1. 如何迁移SharePoint 2010至新的站点

    SharePoint使用非常方便,但是有一个问题获取会困扰大家,就是SharePoint的备份和迁移.下面我们来看一下如何把SharePoint迁移至别的SharePoint站点. 1. 使用网站集管 ...

  2. Wzplayer C++ 版本,WzplayerPro

    WzplayerPro 是Wzplayer的C++版本,新版本支持插件解码器加载等等功能,以上是预览界面. 将会与Wzplayer一样,支持更多的平台,而且WzPlayer在初始化方面将会更快. 联系 ...

  3. Android开发UI之Toast的使用

    Toast,A toast provides simple feedback about an operation in a small popup. 对于操作提供一个简单反馈信息. 官网链接:htt ...

  4. nginx 安全漏洞 (CVE-2013-4547)

    Nginx 的安全限制可能会被某些请求给忽略,(CVE-2013-4547). 当我们通过例如下列方式进行 URL 访问限制的时候,如果攻击者使用一些没经过转义的空格字符(无效的 HTTP 协议,但从 ...

  5. xfce terminal tab被解释为super+tab的bug

    https://bugzilla.xfce.org/show_bug.cgi?id=10760 解决方法 Every few weeks the tab key stops working in th ...

  6. Azure Backup 入门

    Viswanath Tata 云 + Enterprise项目经理 Azure Backup是一款允许客户将数据备份到 Azure的强大工具.请参阅这篇文章,快速了解 Azure Backup.我 ...

  7. C#图片处理之: 另存为压缩质量可自己控制的JPEG

    处理图片时常用的过程是:读入图片文件并转化为Bitmap -> 处理此Bitmap的每个点以得到需要的效果 -> 保存新的Bitmap到文件使用C#很方便的就可以把多种格式的图片文件读到B ...

  8. OAuth 的权限问题与信息隐忧

    核心提示:以 QQ 登陆和微博登陆为代表的“一键登陆”背后不仅仅是登陆这么简单,它还默认获取了你的其他隐私资料和账号的部分使用权限,我们在享受便利的同时一定不要忘记保护好我们的个人信息安全. 去年3Q ...

  9. NullableKey:解决Dictionary中键不能为null的问题 zt

    2012-12-29 02:26 by 老赵, 1745 visits 众所周知,.NET中Dictionary的键不能为null,否则会抛出NullReferenceException,这在某些时候 ...

  10. 基于Geoserver配置多图层地图以及利用uDig来进行样式配置

    在GeoServer中配置多个图层的地图相对来说很容易,其步骤为: 1. 进入geoserver 2. 配置相关的FeatureTypes 3. 配置WMS内容,进入以后,主要有以下几个地方需要命名: ...