Top 10 Questions about Java Exceptions--reference

时间:2022-11-04 18:14:43

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 的前十的方法.他们在*最大投票的问题. 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 &lpar;You Won’t BELIEVE the Last One&rpar;--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. js动态改变时间

    <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="C ...

  2. 20145218 《Java程序设计》第01次实验报告

    北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.08 实验名称:Java开发环境的熟悉(Linux + Eclipse) 实 ...

  3. bzoj1863&colon; &lbrack;Zjoi2006&rsqb;trouble 皇帝的烦恼

    白书原题.l边界又设错啦.一般都是错这里吧.注意为什么这里不能是l=0.(只是为了判断第一个和最后一个 #include<cstdio> #include<cstring> # ...

  4. &lbrack;Android&rsqb;ADT Run时候报错:The connection to adb is down&comma; and a severe error has occured

    The connection to adb is down, and a severe error has occured. 之 ..\sdk\platform-tools\adb.exe and c ...

  5. Vulkan Tutorial 05 物理设备与队列簇

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 Selecting a physical device 通过VkInstance初始 ...

  6. 如何快速禁用约束 &lpar;解决ORA-O2266问题&rpar;

    业务场景: 某天truncate大量表时遇到错误ORA-O2266:表中的唯一/主键被启用的外键引用,不想看ER图来禁用大量复杂约束,所以研发了三种套路...   一. 最偷懒套路 如果要一次性导出很 ...

  7. pandas 基本操作

    1.     一维数据结构Series a.   概念:Series 是pandas 的一维数据结构,有重要的两个属性 index 和values b.  初始化: 可以通过 python 的 Lis ...

  8. 2&period;5 Visio2007不规则图形填充

    1.确保线和线接口的地方正好相交,没有多出来的线: 2.将图形选中>组合: 3.选中图形>形状>操作>连接>填充颜色. 因为图形式几条线段拼合的,不是封闭图形,所以需要将 ...

  9. 查看windows下指定的端口是否开放

    有时候会出现ip  ping的通   但是就是连接不上的情况.这时候我们需要检测一下这个端口是否被开放 netstat -ano -p tcp | find >nul && ec ...

  10. P4292 &lbrack;WC2010&rsqb;重建计划

    无脑上二分+淀粉质完事了 每个子树算的时候把儿子按照最长路径从小到大依次做,和前面的单调队列算一波,每个儿子的复杂度不超过这个子树大小 // luogu-judger-enable-o2 #inclu ...