Beginners understand the basics of java exception handling

  • 2021-10-24 22:44:53
  • OfStack

Directory 1. Exception Architecture 1. What is Exception 2. Exception Architecture 2. Exception Handling throw and throws3. Custom Exception Runtime Exception Compile Time Exception Summary

1. Exception architecture

1. What is an anomaly

In the process of java program running, one unexpected situation occurs, that is, exception. In java, one abnormal one can be divided into two categories:

(Error) Error and (Exception) Exception.

For (error) Error, we can't solve all the errors through the program, we can only try to catch these errors, but we can't deal with them. Often, the occurrence of errors is a major fatal problem for the program, which needs to be corrected through major adjustments. For (exception) Exception, it is something we can handle by program, and we can catch and handle these exception situations.

2. Architecture of exceptions

​ Throwable

​ Error Exception

​ RutimeException Compile-time exception

Description: RutimeException And 1 compile-time exceptions inherit Exception , Exception And Error Inherit Throwable .

Runtime exception ( RutimeException ) We often do not need to catch it in code, but by handling the logic of the code to avoid runtime exceptions. 5 Common runtime exceptions:

ClassCastException (Class conversion exception) ​ Error Exception0 (Array out-of-bounds exception) NullPointerException (Null pointer) ArrayStoreException (Exception of data storage, type is not 1 when manipulating array) NumberFormatException Numeric formatting exception

public class Demo{
    public void test(){
        // This code declares that the 1 Runtime exceptions 
       throw new RutimeException();
    }
}
public class DemoTest{
    public static void main(String[] args){
       Demo demo = new Demo()
           demo.test();
    }
}

As can be seen from the above code, when the runtime exception is generated, the code can be compiled and run normally, but the running result will throw a runtime exception.

Explanation: Runtime exceptions propagate upwards.

2. Exception handling

When an exception occurs, we need to handle it. There are two ways to handle an exception:

One is captured by try … catch … finally;

The other one is thrown by throws;

try... catch... finally

Grammar


try {
//  Code blocks where exceptions may occur 
} catch (/*  Exception classes to be caught  */) {
//  Handling code block after catching exception 
} finally {
//  This code block is executed regardless of whether an exception occurs or not 
}

Example:


public class Demo1{
    public static void main(String[] args){
        String[] strs = {" Zhang 3"," Li 4"," Wang 5"};
        try {
            System.out.println("-------");
            System.out.println(strs[3]); //  An exception occurs here, and when the exception occurs, the subsequent code will not be executed to 
                for (String str : strs) {
                    System.out.println(str);
                }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("++++++++++");
        } catch (Exception e) {
            System.out.println("++++++++++");
        }finally{
            System.out.println("--------------");
        }
    }
}

In try... catch... statement, try statement produces the code behind the exception code will not be executed, produce the exception directly to the catch statement block. catch statement blocks can be written more than one, and catch statement branches look for corresponding types from top to bottom, but note that the parent type must be written after the subtype in the exception type caught by catch.

The finally statement block is executed regardless of whether an exception is generated or not. Commonly used with the release of resources.

throw and throws

throws is one of the exception handling mechanisms, but throw is not; throws is a class that declares exceptions; throw is the object that throws exceptions; throws can declare multiple exception classes, while throw can only throw one exception object; throws is declared on the method, while throw is defined within the method;

Example:


public class Demo{
    public void test() throws Exception{
        // This code declares that the 1 Anomalies 
       throw new Exception();
    }
}
public class DemoTest{
    public static void main(String[] args){
       Demo demo = new Demo()
         try {
           demo.test();
         }catch (Exception e) {
            System.out.println("++++++++++");
        }finally{
            System.out.println("--------------");
        }
    }
}

3. Custom exceptions

Runtime exception

Inheriting RutimeException

Example:


public class MyRutimeException extends RutimeException {
    public MyRutimeException(String message){
        super(message);
    }
}

Compile-time exception

Inheriting Exception

Example:


public class MyException extends Exception {
    public MyException(String message){
        super(message);
    }
}

Summarize

That's all for this article. I hope it will be helpful to you, and I hope you can pay more attention to more contents of this site!


Related articles: