“This is the 26th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

First, what are the types of exceptions in Java

Exceptions are classified into compile-time exceptions (mandatory exceptions, CheckedException) and run-time exceptions (non-mandatory exceptions, RuntimeExceptions) according to the time when they need to be handled.

Only Java provides checked exceptions, and Java believes that checked exceptions can be handled. If you do not handle checked exceptions, the program will report an error at compile time, and the code will never have a chance to execute.

There are two ways to handle checked exceptions:

  1. The current method knows how to handle the exception, so try… Catch block to handle the exception.
  2. If the current method does not know how to handle it, declare that the exception is thrown when the method is defined.

Runtime exceptions only occur when the code is running. Common exceptions include the divisor is 0 and the table below is out of bounds, which causes frequent processing trouble. If the statement or capture is displayed, it has a great impact on the readability and efficiency of the program. So it’s up to the system to automatically detect them and pass them to the default exception handler, or display them if you want to.

What is the return value from calling the following method?

public int getNum(){  
  try {  
    int a = 1/0;  
    return 1;  
} catch (Exception e) {  
    return 2;  
}finally{  
    return 3;  
} 
Copy the code

The code hits a MathException at line 3, at which point line 4 will not execute and will jump directly to the catch statement. Finally, at line 6, one of the rules of the exception mechanism is that if a catch, such as a return or exception, terminates the function, then a finally block must be executed before a value is returned. So the code jumps to line 8. Unfortunately, line 8 is a return statement, and the method ends, so the result of line 6 cannot be returned. If finally only handles a resource release operation, the method returns the value on line 6.

Difference between Error and exception

The error and Exception classes have throwable classes as their parents. The differences are as follows:

Error class generally refers to problems related to virtual machine, such as system crash, virtual machine error, insufficient memory space, method call stack overflow, etc. Application interruption caused by such errors cannot be recovered or prevented by the program itself. It is recommended to terminate the program when such errors occur.

The Exception class represents exceptions that a program can handle to catch possible recovery. This type of exception should be handled as much as possible, so that the program can resume running, rather than arbitrarily terminated exceptions.

The exception class is divided into runtime exceptions and compile-time exceptions, such as ArithmaticException and IllegalArgumentException. The runtime exceptions that the compiler can pass but terminate upon execution are not handled by the program. Compile-time exceptions either use try… Throws are either declared with a throws statement, which is passed to its parent class for processing, or the compilation fails.

Java exception handling mechanism

Java classifies exceptions. Different types of exceptions are represented by different Java classes. The root class of all exceptions is java.lang.throwable.

Two subclasses of Throwable are derived: Error and Exception, where Error represents a serious problem that the application itself cannot overcome and recover from.

Exception refers to the problems that can be overcome and recovered by the program, which can be divided into system exceptions and common exceptions. System exceptions are problems caused by the defects of the software itself, which are caused by the lack of consideration of the software developer. Software users cannot overcome and recover such problems. But this problem can also make the software system to continue running or kill software, for example, an array of script cross-border (ArrayIndexOutOfBoundsException), null pointer exception

(NullPointerException), ClassCastException; Common exceptions are problems caused by changes in the operating environment or exceptions that can be overcome by users. For example, a network disconnection or insufficient disk space should not kill a program after such exceptions occur.

Java provides a different solution for system and normal exceptions, where the compiler enforces a try.. The catch handler or throws a catch declaration to the upper calling method. Therefore, common exceptions are also called checked exceptions. System exceptions may or may not be handled. System exceptions are also called unchecked exceptions because catch is handled or declared with throws.

List the five most common runtimeExceptions

  1. Java. Lang. NullPointerException null pointer exception; Cause: An uninitialized or nonexistent object was called.
  2. Java. Lang. ClassNotFoundException specified class find; Cause: Class name and path loading error; These are usually exceptions that can be thrown when a program tries to load a class through a string.
  3. Java. Lang. A NumberFormatException string is converted to a digital exception; Cause: Character data contains non-numeric characters.
  4. Java. Lang. Angle the cross-border IndexOutOfBoundsException array is unusual, common in operation of the array object.
  5. SQLException SQL exception, common SQL statement error during database operation.
  6. Java. Lang. IllegalArgumentException method passing parameters errors.
  7. Java. Lang. ClassCastException abnormal data type conversion.
  8. Java. Lang. NoClassDefFoundException class definition was not found error.
  9. Java. Lang. Abnormal InstantiationException instantiation.
  10. Java. Lang. NoSuchMethodException method there is no exception.

Throw throws

Throw:

  1. A throw statement is used in the body of a method to indicate that an exception is thrown.
  2. A throw is an action that specifically throws an exception, so it throws an exception instance, and executing a throw must throw some kind of exception.

Throws:

  1. The throws statement is used after a method declaration to indicate that if an exception is thrown, the method caller handles the exception.
  2. Throws throws throws throws throws of a certain type, letting its users know the type of exception to catch.
  3. Throws represents a possibility of an exception that is not necessarily expected.

Vii. Differences between final, finally and Finalize

  1. Final: used to declare an attribute, a method, and a class, indicating that the attribute is immutable, the method cannot be overridden, and the class modified by it cannot be inherited.
  2. Finally: Part of the exception handling statement structure that always executes.
  3. Finalize: A method of the Object class that is called when the garbage collector executes the Object to be collected. You can override this method to provide other resource recycling at garbage collection time, such as closing files. Object of this method is more like a dying method of life cycle, when this method is called system represents the object is “death”, but it is important to note that we take the initiative to act up and call the method will not lead to “death” the object, this is a passive method (in fact, the callback methods), do not need to call us.