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.

References:
1. Unchecked exceptions in Java
2. The root of Java exception class hierarchy
3. Java exceptions related questions in stackoverflow

Category >> Exceptions >> Top 10  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • nice

  • well explain thanks a lots of . must visit java exception examples here

  • vedyzarc

    3) “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”

    That is wrong. Here is code example

    try {
    String s = “inside”;
    FileInputStream fis = new FileInputStream(“path”);
    } catch (FileNotFoundException e) {
    System.out.println(s);
    }

    As you can see FileNotFoundException certainly won’t be thrown before “s” variable declaration and initialization.

    The reason why variables of “try” block are not visible in “catch” or “finally” blocks is because of java variable visibility scope. The same as you can’t use variables out of another method or “while” “for” loop where the variable is declared.

  • xiaobin Zheng

    About question 10. I found a wonderful comment in stackoverflow .Here is the link :http://stackoverflow.com/questions/921471/why-java-people-frequently-consume-exception-silently/921583

  • AA

    Funny as hell!!

  • 939347507

    不明觉历

  • Michael

    Good summary! However, would explain 4 anyway for better understanding, argue with the explanation of 6 and give examples where 10 might be okay 😉