Top 10 Questions about Java Exceptions

In the review, I synthesized the learning notes of the excellent lessons and summarized the basic knowledge.Top 10 common questions about Java exceptions are summarized.Code a lot of professional knowledge, to share with you for reference.

1. Checked and unchecked

In short, the checked exception must be explicitly caught in the method or declared in the throws clause of the method.Unchecked exceptions are caused by problems that cannot be resolved, such as division by zero, null pointers, and so on.Checking for exceptions is especially important because you want other developers using the API to know how to handle exceptions.
For example, IOException is a commonly checked exception, while RuntimeException is an unchecked exception.You can check the Java exception hierarchy diagram before reading the rest.

2. Best practices for exception management

If the exception can be handled correctly, it should be caught or thrown.

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

In the following code, the string s declared in the try block cannot be used in the catch clause.This code does 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 you don't know where in the try block is causing an exception.It is very likely that an exception was thrown before the object was declared.This is correct 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 JDK problem.They were developed by different developers, so it's not worth thinking about.

Integer.parseInt(null); 
// throws java.lang.NumberFormatException: null
 
Double.parseDouble(null); 
// throws java.lang.NullPointerException
  1. Runtime exceptions commonly used in Java

This is just some of them.

IllegalArgumentException
ArrayIndexOutOfBoundsException

When the conditions are not met, you can use them in the if statement 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 these exception classes can be traced back to the same superclass in the class inheritance hierarchy, they can only be used.

7. Can constructors cause exceptions in Java?

The answer is yes.Constructors are a special method.This is a code example.

8. Raise an exception in the last 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();
        }
    }
}

However, for better code readability, you should wrap the embedded try-catch block as a new method and place the method call in the final 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 be used in finally blocks

Yes, it can.

10. Why do developers use exceptions silently?

A number of time code snippets have occurred similar to the following.If it is important to handle exceptions correctly, why do developers continue to do so?

try {
     ...
} catch(Exception e) {
     e.printStackTrace();
}

The article is shared here, and you are welcome to add and comment.
If you like this article, please leave a comment and remember to pay attention to me, keep updating technical drinks, job interesting, lots of interview materials and so on.
As well as the complete set of java architecture thinking maps of recent hematemesis, you can refer to and learn by comparing points of knowledge.

Tags: Java REST JDK

Posted on Thu, 09 Jan 2020 09:12:35 -0800 by imagineek