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 SeekBar实现音量调节
SeekBar可以通过滑块的位置来标识数值----而且拖动条允许用户拖动滑块来改变值,因此拖动条通常用于对系统的某种数值进行调节,比如调节音量等. SeekBar允许用户改变拖动条的滑块外观,改变滑块 ...
- css揭秘之linear-gradient
很神奇的背景设置, 看代码, <html> <title>css</title> <style> .content { background: line ...
- 谈谈分布式事务之三: System.Transactions事务详解[上篇]
在.NET 1.x中,我们基本是通过ADO.NET实现对不同数据库访问的事务..NET 2.0为了带来了全新的事务编程模式,由于所有事务组件或者类型均定义在System.Transactions程序集 ...
- 远程连接centos
yum install tigervnc yum install tigervnc-server Windows 7下载客户端 tigervnc-1.2.0.exe,在http://sourcef ...
- 基于duilib实现的可滑动tab标签控件
最近一直在忙棋牌游戏大厅的开发,使用了duilib界面库,在大厅界面游戏菜单的展现上需要用到滑动的效果,类似悠扬棋牌,jj棋牌的菜单左右(上下)滑动的效果.通过自己的设计思路完善了一个可滑动的tab标 ...
- mysql 资料总结 长期更新
http://blog.csdn.net/ww1982_0_0_0/article/details/9169613 引入employess
- poj2192(搜索)
这个题目对于两个字符串A,B是否可以通过规则生成C. import java.util.Scanner; public class Main { public static void main(Str ...
- oracle logminer全解析
今天写篇原创的,把在工作中遇到的logminer问题总结下 (1)简介: logminer 工具即可以用来分析在线,也可以用来分析离线日志文件,即可以分析本身自己数据库的重作日志文件,也可以用来分析其 ...
- NOIP2015 斗地主(搜索+剪枝)
4325: NOIP2015 斗地主 Time Limit: 30 Sec Memory Limit: 1024 MBSubmit: 270 Solved: 192[Submit][Status] ...
- 洛谷P1157 组合的输出
洛谷1157 组合的输出 题目描述 排列与组合是常用的数学方法,其中组合就是从n个元素中抽出r个元素(不分顺序且r<=n),我们可以简单地将n个元素理解为自然数1,2,…,n,从中任取r个数. ...