JavaRush/Java Blog/Random EN/Top 10 Java Exception Questions
raynn
Level 31

Top 10 Java Exception Questions

Published in the Random EN group
members
The article describes the 10 most frequently asked questions about exceptions in Java. Top 10 Java Exception Questions - 1

1. Verifiable and unverifiable

In short, checked exceptions must be explicitly caught in the method body, or declared in the method's throws section. Unchecked exceptions are caused by problems that cannot be solved, such as division by 0, the null pointer, and so on. Checked exceptions are especially important because you expect other developers using your API to know how to handle exceptions. For example, IOException is a commonly used checked exception, while RuntimeException is an unchecked one. Before reading further, take a look at the Hierarchical Exception Diagram in Java .

2. The best way to deal with exceptions

If the exception can be handled correctly, it must be caught, otherwise it must be re-thrown.

3. Why can't variables defined in try be used in catch or finally?

In the following piece of code, the string s declared in a try block cannot be used in a catch block. This code will not compile.
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 it is not known exactly where in the try block the exception could have been thrown. It is possible that the exception was thrown before the object was declared. And it is true for this example.

4. Why do Double.parseDouble(null) and Integer.parseInt(null) throw different exceptions?

Indeed, they do raise different exceptions. This is a JDK issue. They just were developed by different people, and you should not seriously bother with it.
Integer.parseInt(null);
// вызывает java.lang.NumberFormatException: null

Double.parseDouble(null);
// вызывает java.lang.NullPointerException

5. Basic runtime exceptions in Java

Here are a few of them:
IllegalArgumentException
ArrayIndexOutOfBoundsException
They can be used in an if statement when the condition is not met, like this:
if (obj == null) {
   throw new IllegalArgumentException("obj не может быть равно null");

6. Is it possible to catch multiple exceptions in one catch block?

The answer is YES. As long as these exception classes can be traced back up the class inheritance hierarchy to the same superclass, only that superclass can be used.

7. Can a constructor throw exceptions?

The answer is YES. A constructor is just a special kind of method. Here is a sample code.

8. Throwing exceptions in the final block

In principle, it is quite legal to do this:
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 keep the code readable, you need to declare the nested block try-catchas a new method, and insert a call to this method in the block finally.
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. Is it possible to use return in a finally block

Yes, you can.

10. Why do developers silently handle exceptions?

Often there are, for example, such pieces of code. If proper exception handling is so important, why do developers keep writing like this?
try {
     ...
} catch(Exception e) {
     e.printStackTrace();
}
It's easy to ignore. But even if this is often done, it does not mean that it is correct. Links:
  1. Unchecked Exceptions in Java
  2. Root of the hierarchical exception tree in Java
  3. stackoverflow questions about exceptions
Original article
What else to read:

Exceptions in Java

Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet