Abstract: Java exception refers to some errors that may occur when the program is running, such as: file cannot be found, network connection failure, illegal parameters, etc. An exception is an event that occurs during program execution and interrupts the normal instruction flow of the executing program.

This article is shared by Huawei Cloud community “Java Knowledge points problem selection exception mechanism”, original author: breakDraw.

Java exceptions refer to errors that may occur during program execution, such as file failure, network connection failure, and invalid parameters. An exception is an event that occurs during program execution and interrupts the normal instruction flow of the executing program.

Java describes various exceptions through a number of subclasses of the Throwable class in the API. Thus, Java exceptions are objects, instances of Throwable subclasses that describe error conditions that occur in a piece of code. When a condition is generated, an error will throw an exception. Java technology requires different exception handling for runtime exceptions, errors, or traceable exceptions.

Classification of anomaly system

Q: Relationship between Throwable and Error

A: Throwable is the base class for Error, as well as Exception

1 good graph, common exceptions and errors can be seen

Q: Relationship between Error and Exception

A:

  • An Error is an Error that directly causes a JVM Error, such as a Java VIRTUAL machine running Error. If the Error occurs, the current thread cannot continue running.

  • Excpetion is an exception that the program itself can handle. It still works after it happens.

Q: Can errors be caught by a catch?

A: Throwable and its subclasses can throw and catch. But catching errors is not recommended.

Anomaly systems can also be divided into the following two categories:

  • Unchecked Exception: unchecked exception

Also known as a runtime exception (RuntimeException), such as common NullPointerException, IndexOutOfBoundsException. For runtime exceptions, the Java compiler does not require exception catching or throw declarations; it is up to the programmer.

  • Checked exception

Also known as non-runtime exceptions (exceptions other than runtime exceptions are non-runtime exceptions), the Java compiler forces the programmer to catch exceptions, such as the common IOExeption and SQLException. Compilation will not pass without capturing or throwing a non-runtime exception.

Exception capture and return

Q: return-finally Trap 1: Can finally update the value of a return variable by modifying a variable

int f() {  int a = 1;  try {      return a;  }  finally {      a=2;  }}
Copy the code

A: No, f returns 1.

Q: return-finally Trap 2: Finally when also return, which return?

int f() {  try {      return 1;  }  finally {      return 2;  }}
Copy the code

A: Return in finally, return 2.

Q: When can a step in finally block not be executed?

A: You can make finally not execute only by calling System.exit(0) to exit the JVM before finally.

Q: What happens next?

try { start(); } catch (Exception ex) { System.out.println("catch Exception"); } catch (RuntimeException re) { System.out.println("catch RuntimeException"); }Copy the code

A: Compiling directly is wrong. The catch is sequential and once a RuntimeExcpetion is matched no longer, so the compiler recognizes that the RuntimeExcpetion will never be caught and reports an error in advance.

Q: When throwing an exception, finally does a return. Will the exception still be thrown?

static int f() { try { int a = 1/0; return a; } catch (Exception e) { throw new RuntimeException(e); } finally { return -1; }}public static void main(String[] args) { System.out.println(f()); }Copy the code

A: No, return -1.

A return in finaly interrupts a throw

So never return in finally

Problems related to abnormal inspection

Q: When a subclass overwrites a base-class method, can it throw exceptions that do not exist in the base-class method?

Something like this:

class A{    void f() throws IOException{    }}class B extends A{    void f() throws IOException, SQLException {    }}
Copy the code

A: No, direct compilation error. That is, when a subclass overwrites a parent class method, the exception followed by the throws keyword must be less than or equal to the exception of the parent class method.

Q: A call to a resource close in finally also throws a checked exception. What else can you do in the finally besides try-catch?

In the end, finally has a catch.

TryWithResource tryWithResource = new TryWithResource(); try { System.out.println(tryWithResource.age); } catch (Exception e) { e.printStackTrace(); }finally { try { tryWithResource.close(); } catch (Exception e) { e.printStackTrace(); }}Copy the code

A: if JDK1.7, you can use the try-with-resource syntax.

The resource class is required to implement the AutoCloseable interface and follow the creation of the resource after the try parenthesis when a try is executed, as follows:

public static void main(String[] args) { try (TryWithResource tryWithResource = new TryWithResource()) { System.out.println(tryWithResource.age); } catch (Exception e) { e.printStackTrace(); }}Copy the code

There is no need to write finally, finally+close will be added to us automatically by the compiler.

Q: How do I catch exceptions thrown by threads?

A: implement MyUnchecckedExceptionhandler exception handling interface

public class MyUnchecckedExceptionhandler implements UncaughtExceptionHandler { @Override public void UncaughtException (Thread t, Throwable e) {system.out.println (" threadexception: "+ e); }}Copy the code

Then set the implementation class to the corresponding thread.

Thread t = new Thread(new ExceptionThread()); t.setUncaughtExceptionHandler(new MyUnchecckedExceptionhandler()); t.start();Copy the code

Click to follow, the first time to learn about Huawei cloud fresh technology ~