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 ...
随机推荐
- Android 用Intent和Bundle传递参数
传递方: //点击btn_sub传递 fieldHeight.getText()和 fieldWeight.getText() private void setListeners() { ...
- Form.block Trigger DML常规写法
Block的数据源是View的,如果想操作数据,需要注意在以下几个Trigger里面写代码: 一般建议创建View的时候包含rowid字段. on-lock: select inventory_ite ...
- Hibernate一级缓存、二级缓存
缓存就是把以前从数据库中查询出来和使用过的对象保存在内存中,准确说就是一个数据结构中,这个数据结构通常是或类似HashMap,当以后要使用某个对象时,先查询缓存中是否有这个对象,如果有则使用缓存中的对 ...
- 软件介绍(apache lighttpd nginx)
一.软件介绍(apache lighttpd nginx) 1. lighttpd Lighttpd是一个具有非常低的内存开销,cpu占用率低,效能好,以及丰富的模块等特点.lighttpd是众多 ...
- c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...
- ZOJ3261 Connections in Galaxy War 并查集
分析:对于这种删边操作,我们通常可以先读进来,然后转化离线进行倒着加边 #include <stdio.h> #include <string.h> #include < ...
- 【转】Compile FFmpeg on CentOS 6.x
This guide is based on a minimal CentOS installation and will install FFmpeg with several external e ...
- editpuls查找替换通配符
1 \t Tab character. tab符号 2 \n New line. 新的一行(换行符) 3 . Matches any ...
- Apple LLVM 6.0 Warning: profile data may be out of date
I have no clue what this meant, so I googled the problem. I only ended up with some search results s ...
- html5 做游戏 Quintus Sublime Text牛逼的神器