Abnormal (7)

74. The difference between throw and throws?

Throws is used to declare all the exception information that may be thrown by a method. Throws is to declare the exception but not handle it, but upload the exception, whoever calls it, I will handle it. And throw refers to a specific type of exception thrown.

75. What is the difference between final, finally, and finalize?

  • Final can modify classes, variables, and methods. Modified classes indicate that the class cannot be inherited, modified methods indicate that the method cannot be overridden, and modified variables indicate that the variable is a constant and cannot be reassigned.
  • Finally is generally used in the try-catch code block. When dealing with exceptions, usually we must execute the code method in the finally code block, which means that the code block will be executed regardless of whether there is an exception or not, and it is generally used to store some closed resources Code.
  • Finalize is a method that belongs to a method of the Object class, and the Object class is the parent class of all classes. This method is generally called by the garbage collector. When we call the gc() method of the System, the garbage collector calls finalize (),recycle trash.

76. Which part of try-catch-finally can be omitted?

Answer: catch can be omitted

the reason:

A more strict statement is actually: try is only suitable for handling runtime exceptions, try+catch is suitable for handling runtime exceptions + ordinary exceptions. In other words, if you only use try to handle ordinary exceptions without catch processing, the compilation will not pass, because the compiler has a rigid rule that if you choose to catch ordinary exceptions, you must use catch to display the declaration for further processing. The runtime exception is not so stipulated at compile time, so catch can be omitted, and you add the catch compiler to think it is understandable.

In theory, the compiler looks at any code that is not pleasing to the eye and feels that there may be potential problems, so even if you add try to all the code, the code is just a layer of skin on the basis of normal operation during the runtime. But once you add try to a piece of code, it is equivalent to an explicit promise to the compiler to catch exceptions that this piece of code may throw instead of throwing it upwards. If it is a normal exception, the compiler requires that it must be caught by catch for further processing; if it is abnormal at runtime, it is caught and then discarded and processed with +finally, or a catch is added for further processing.

As for adding finally, it is the "finishing" processing that must be carried out regardless of whether the exception is caught or not.

77. In try-catch-finally, if the catch is returned, will finally be executed?

Answer: It will be executed, and it will be executed before return.

Code example 1:

/*
 * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
 */
public class FinallyDemo2 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(getInt());
    }

    public static int getInt() {
    
    
        int a = 10;
        try {
    
    
            System.out.println(a / 0);
            a = 20;
        } catch (ArithmeticException e) {
    
    
            a = 30;
            return a;
            /*
             * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了
             * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
             * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30
             */
        } finally {
    
    
            a = 40;
        }

//      return a;
    }
}

1234567891011121314151617181920212223242526272829

Implementation results: 30

Code example 2:

package com.java_02;

/*
 * java面试题--如果catch里面有return语句,finally里面的代码还会执行吗?
 */
public class FinallyDemo2 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println(getInt());
    }

    public static int getInt() {
    
    
        int a = 10;
        try {
    
    
            System.out.println(a / 0);
            a = 20;
        } catch (ArithmeticException e) {
    
    
            a = 30;
            return a;
            /*
             * return a 在程序执行到这一步的时候,这里不是return a 而是 return 30;这个返回路径就形成了
             * 但是呢,它发现后面还有finally,所以继续执行finally的内容,a=40
             * 再次回到以前的路径,继续走return 30,形成返回路径之后,这里的a就不是a变量了,而是常量30
             */
        } finally {
    
    
            a = 40;
            return a; //如果这样,就又重新形成了一条返回路径,由于只能通过1个return返回,所以这里直接返回40
        }

//      return a;
    }
}

1234567891011121314151617181920212223242526272829303132

Implementation results: 40

78. What are the common exception classes?

  • NullPointerException: This exception is thrown when the application tries to access a null object.
  • SQLException: Provides exceptions about database access errors or other error information.
  • IndexOutOfBoundsException: It is thrown when indicating that a sort index (for example, the sorting of an array, string, or vector) is out of range.
  • NumberFormatException: This exception is thrown when the application tries to convert a string into a numeric type, but the string cannot be converted into an appropriate format.
  • FileNotFoundException: This exception is thrown when an attempt to open the file indicated by the specified path name fails.
  • IOException: When a certain I/O exception occurs, this exception is thrown. This class is a general class of exceptions generated by failed or interrupted I/O operations.
  • ClassCastException: This exception is thrown when trying to cast an object to a subclass that is not an instance.
  • ArrayStoreException: An exception thrown when trying to store an object of the wrong type in an object array.
  • IllegalArgumentException: The thrown exception indicates that an illegal or incorrect parameter was passed to the method.
  • ArithmeticException: This exception is thrown when an abnormal operation condition occurs. For example, when an integer is "divided by zero", an instance of this class is thrown.
  • NegativeArraySizeException: If the application attempts to create an array with a negative size, this exception is thrown.
  • NoSuchMethodException: This exception is thrown when a specific method cannot be found.
  • SecurityException: An exception thrown by the security manager, indicating that there is a security violation.
  • UnsupportedOperationException: This exception is thrown when the requested operation is not supported.
  • RuntimeExceptionRuntimeException: is the superclass of exceptions that may be thrown during the normal operation of the Java virtual machine.

Guess you like

Origin blog.csdn.net/xghchina/article/details/114902721