Exception Handling in Java Interviews

1. What is the difference between error and exception?

The parent class of Error class and Exception class is Throwable class

The Error class generally refers to problems related to the virtual machine, such as system crash, virtual machine error, insufficient memory space, method call stack overflow, etc. The application interruption caused by the error cannot be recovered and prevented by the program itself. When such an error is encountered, it is recommended to terminate the program.

The Exception class represents an exception that a program can handle, catch and potentially recover from. When this kind of exception is encountered, the exception should be handled as much as possible to make the program resume operation, instead of terminating the exception at will.

2. Java exception handling mechanism

Java classifies exception classes. Different types of exceptions are represented by different Java classes. The root class of all exception classes is java.lang.Throwable. There are two derived subclasses Error and Exception under Throwable. Error represents the application program. A serious problem that cannot be overcome and recovered by itself. Exception represents the problems that the program can recover and overcome, which are divided into system exceptions and ordinary exceptions. System exceptions are problems caused by the defects of the software itself, that is, problems caused by developers' inattentive consideration, and software users cannot overcome and recover. However, this kind of problem can also make the system run or the software die, for example, array index out of bounds (ArrayIndexOfBoundsException), null pointer exception, etc.; common exceptions are problems caused by changes in the operating environment or exceptions, and are problems that users can overcome , for example, the network is disconnected, the hard disk space is not enough, after such an exception occurs, the program should not die.

Java provides different solutions for system exceptions and ordinary exceptions. The compiler enforces that ordinary exceptions must be handled by try..catch or continue to be thrown to the calling method with the throws statement, so ordinary exceptions are also called checked exceptions, while the system Exceptions can be handled or not handled, so the compiler does not enforce try..catch handling or throws declarations, so system exception handling is also called uncheched exceptions.

3. Write out 5 common RuntimeExceptions

     (1) java.lang.NullPointerException Null Pointer Exception, Cause: An uninitialized object or a non-existent object was called.

     (2) ClassNoFoundException The specified class cannot be found. The reason: the name and path of the class are loaded incorrectly. Usually, an exception may be thrown when trying to load a class through a string.

     (3) NumberFormatException The string is converted to a number exception, the reason: the string data contains non-numeric characters.

     (4) IndexOutOfBoundsException array subscript out of bounds exception

     (5) IllegalArgumentException method passed parameter error

      (6) ClassCastException data type conversion exception

      (7)NoClassDefFoundExcetion class definition not found error

      (8) SQLException SQL exception

       (9) InstantiationException instantiation exception

        (10) There is no exception in the NoSuchMethodExceptionin method

6. The difference between throw and throws

throw:

    (1) The throw statement is used in the method body, indicating that an exception is thrown, which is handled by the statement in the method body

    (2) throw is an action that throws an exception to the outside, so it throws an exception instance, and the execution of throw must throw some kind of exception

throws:

    (1) The throws statement is used after the method declaration, indicating that if an exception is thrown, the caller of the method will handle the exception.

    (2) throws mainly declares that this method will throw a certain type of exception, so that its users need to know the type of exception that needs to be caught.

    (3) throws indicates a possibility of an exception, which does not necessarily occur.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325477642&siteId=291194637