Java learning -- JavaSE -- exception handling

Abnormal mechanism Java treats exceptions as objects and defines a base class java.lang.Throwable as the superclass of a...
Abnormal mechanism

Abnormal mechanism

  • Java treats exceptions as objects and defines a base class java.lang.Throwable as the superclass of all exceptions.
  • Many Exception classes have been defined in the Java API. These Exception classes are divided into two categories: Error and Exception

Error

  • The Error class object is generated and thrown by the Java virtual machine. Most errors have nothing to do with the operation performed by the coder.

  • Java virtual machine running error. OutOfMemoryError will appear when the JVM no longer has the memory resources required to continue the operation. When these exceptions occur, the Java virtual machine (JVM) will generally choose thread termination;

  • In addition, when the virtual machine attempts to execute the application, such as class definition error (NoClassDefFoundError) and link error (LinkageError). These errors are not traceable because they are outside the control and processing power of the application, and most of them are not allowed when the program is running.

Exception

  • There is an important subclass runtimeException (runtime Exception) in the Exception branch
    • Arrayindexoutofboundexception (array subscript out of bounds)
    • NullPointerException (null pointer exception) arithmeticexception (arithmetic exception)
    • Missingresourceexception (missing resource)
    • Classnotfoundexception (class not found) and other exceptions. These exceptions are not checked. The program can choose to capture and handle them or not.

Difference between Error and Exception: Error is usually a catastrophic and fatal Error that cannot be controlled and handled by the program. When these exceptions occur, the Java virtual machine (JVM) will generally choose to terminate the thread; Exception can usually be handled by the program, and these exceptions should be handled as much as possible in the program.

Exception handling mechanism

*Throw exception:

*Catch exception:

*There are five keywords for exception handling:

  • try,catch,finally,throw,throws.
public static void main(String[] args) { int a=1; int b=0; //To catch multiple exceptions, the exception type should be from small to large //throwable>error|exception>...... try { if (b==0){//Actively throw exception throw new Exception(); } System.out.println(a/b); } catch (Exception e) {//Catch (the type of exception you want to catch), System.out.println("The program has an exception" + e); } catch (Throwable t) { System.out.println("An exception occurred"+t); }finally {//It is not necessary to deal with the aftermath work. It is generally used to close the flow System.out.println("finally"); } }

Shortcut key: ctrl+alt+t

Method throws an exception

public static void main(String[] args) { try { new Test().test(1,0); } catch (Exception e) { e.printStackTrace(); } } public void test(int a,int b) throws Exception{ if (b==0){//Actively throw exception throw new Exception(); } System.out.println(a/b); }

Custom exception

  • Custom Exception. You can customize the Exception class by inheriting the Exception class.

  • Using custom exception classes in programs can be roughly divided into the following steps:

    • Create a custom exception class.
    • Throw an exception object through the throw keyword in the method.
    • If the exception is handled in the method that currently throws the exception, you can use the try catch statement to catch and handle it; otherwise, indicate the exception to be thrown to the method caller through the throws keyword in the method declaration, and continue to the next step.
    • Catch and handle exceptions in the caller of the exception method.
public class MyExecption extends Exception { //Number of passes > 0 private int detail; //Shortcut key alt+insert Constructor public MyExecption(int a) { this.detail=a; } @Override public String toString() { return "MyExecption{"+detail+"}"; } } public class Test { static void test(int a) throws Exception{ if (a>10){ throw new MyExecption(a);//Throw } System.out.println("ok"); } public static void main(String[] args) { try { test(11); } catch (Exception e) { System.out.println(e); } } }

Reminder:

  • When handling runtime exceptions, logic is used to reasonably avoid and assist in try catch processing
  • After multiple catch blocks, you can add a catch (Exception) to handle exceptions that may be missed
  • For uncertain code, you can also add try catch to handle potential exceptions
  • Try to handle exceptions, and never simply call printStackTrace() to print out
  • How to handle exceptions should be determined according to different business requirements and exception types
  • Try to add finally statement blocks to release the occupied resources

24 September 2021, 05:21 | Views: 6484

Add new comment

For adding a comment, please log in
or create account

0 comments