JavaRush /Java Blog /Random EN /Top 10 Questions about Exceptions in Java
raynn
Level 31
Нижний Новгород

Top 10 Questions about Exceptions in Java

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

1. Verifiable and unverifiable

In short, checked exceptions must be explicitly caught in the method body or declared in the throws section of the method. Unchecked exceptions are caused by problems that cannot be solved, such as division by 0, null pointer, etc. 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 exception. Before reading further, check out Hierarchical Exception Diagram in Java .

2. Best way to deal with exceptions

If the exception can be handled correctly, it must be caught, otherwise it must be forwarded.

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

In the following piece of code, the line 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 where exactly in the try block the exception might have been thrown. It is possible that the exception was thrown before the object was declared. And this is true for this example.

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

That's right, they do raise different exceptions. This is a JDK issue. They were simply designed by different people, and you shouldn't worry too much about 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 here:
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 the classes of these exceptions can be traced 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 code example.

8. Throwing exceptions in the final block

In principle, you can quite legally 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 maintain readability of the code, you need to declare the nested block try-catchas a new method, and insert a call to this method into 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 handle exceptions quietly?

For example, such pieces of code often appear. If proper exception handling is so important, why do developers continue to write it this way?
try {
     ...
} catch(Exception e) {
     e.printStackTrace();
}
It's easiest to ignore. But even if this is often done, it does not mean that it is correct. Links:
  1. Unchecked Exceptions in Java
  2. The root of a hierarchical exception tree in Java
  3. Questions about exceptions on stackoverflow
Original article
What else to read:

Exceptions in Java

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION