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.IllegalArgumentExceptionArrayIndexOutOfBoundsException
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 ...
随机推荐
- ALT+数字,可输入汉字或拉丁字母 good
各种编码查询表:http://bm.kdd.cc/ 输入 ALT + 50385 就出 难 字了,原因是它的十六进制ANSI编码是C4D1=50385 同理: 汉=BABA=47802字=D7D6=5 ...
- Android list1去除list2中的元素
public static void main(String[] args) { List<String> firList = new ArrayList<String>(); ...
- Tomcat部署(转)
首先说说tomcat的几种部署方法: 1.将应用文件夹或war文件塞到tomcat安装目录下的webapps子目录下,这样tomcat启动的时候会将webapps目录下的文件夹或war内容当成应用部署 ...
- jni数据问题
目的: jni中(c++函数)一个 char buf[4] 如何通过env->CallVoidMethod(clazz,method_OnFindCards,jStringParam); 在ap ...
- BZOJ_3282_Tree_(LCT)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3282 给出n个点以及权值,四种操作: 0.求x,y路径上的点权值的异或和. 1.连接x,y. ...
- 宣布正式发布 Azure 媒体服务内容保护服务
Mingfei Yan Azure媒体服务项目经理 我们非常高兴地宣布正式发布 Azure 媒体服务内容保护服务.这包括 Microsoft PlayReady许可服务和 AES明文密钥交付服务!此外 ...
- 兼容ie\firefox\chrome的cursor
cursor:hand 与 cursor:pointer 的效果是一样,都像手形光标. 但用FireFox浏览时才注意到使用cursor:hand在FireFox.chorme里并被支持.cursor ...
- Eclipse工具使用技巧总结
首先推荐一篇非常好的How to use eclipse文章 ,讲的是eclipse使用的方方面面,非常实用,推荐给大家! 一.常用快捷键:Ctrl+F11 运行Ctrl+Shift+/ 在代码窗口中 ...
- html的两种提交按钮submit和button
转自:http://baiying.blog.51cto.com/1068039/1319784 html按钮有两种: <input type="button" value= ...
- LightOJ 1214 Large Division 水题
java有大数模板 import java.util.Scanner; import java.math.*; public class Main { public static void main( ...