Home > Java > javaTutorial > Methods and steps for analyzing Java exception Exception instances

Methods and steps for analyzing Java exception Exception instances

WBOY
Release: 2023-04-20 15:31:26
forward
1473 people have browsed it

1. The largest parent class of exceptions, Throwable

Methods and steps for analyzing Java exception Exception instances

Throwable can be divided into two types:

Error

Exception:

  • Run-time exception (unchecked exception)

  • Non-run-time exception (checked exception)

Error: Error is an error that the program cannot handle. It is thrown by the jvm, such as OutOfMemoryError, ThreadDeath, etc. When these exceptions occur, the jvm will choose to terminate the program execution

Exception: It is the program Exceptions that can be handled by itself

  • Runtime exceptions: They are all RuntimeException classes and subclasses, such as NullPointerException (null pointer), IndexOutOfBoundException (array out of bounds), etc. These exceptions can be captured and processed. Or you can not handle

  • Non-runtime exceptions: (checked exceptions) Except for RuntimeException, the type belongs to the Exception class. From the perspective of program syntax, these exceptions must be processed. If not handled, the program will not be able to pass compilation, such as IOException, SQIException

2, try-catch-finally three statements to pay attention to

  • try, catch, and finally cannot be used alone. The three can be used in combination. The combination method is: try…catch, try…catch…finally, try…finally. There can be one or more catches, and there can only be one finally statement at most.

  • The scope of the variables in the three statement blocks is the code block. They cannot access each other independently. If you want to be accessible in all three blocks, define the variables outside these blocks.

  • If there are multiple catch blocks, at most one exception class will be matched and the catch block code will be executed, and other catch blocks will not be executed, and the matching order is from top to bottom. , only one exception class will be matched at most, that is to say, none of the exception classes can be executed.

  • catch The captured exception class cannot be repeated, and the subclass exception must be captured first and then the parent class. Exception

  • Regardless of whether there is a problem with the statement in try, the statement in finally must be executed. finally provides a unified exit for the program to ensure unified management of program status. Under normal circumstances, resources are processed in finally Closing and clearing work

package com.exception;
public class Test {
    public static void main(String[] args) {
        int[] a=new int[5];
        try {
            for (int i = 0; i <10 ; i++) {
                a[i]=i;
            }
            //空指针
        }catch (ArrayIndexOutOfBoundsException e){
            //弹出出错的地方
            System.out.println("1");
            e.printStackTrace();
            //访问异常,数组越界
        }catch (NullPointerException e){
            System.out.println("2");
            e.printStackTrace();
            //无论有错与否,finally最终执行
        } finally {
            System.out.println("都能执行");
        }
        System.out.println("---------------");
    }
}
Copy after login

3. The functions of final-finally-finalize

  • final: keyword used to define constants

  • finally: a code block that can always be executed in an exception

  • finalize(): garbage collection, this method is a system call and does not need to be called manually by the programmer

4. Throws keyword

  • When defining a method, you can use the throws keyword statement to indicate that the method will not handle the exception. Instead, it is left to the method caller to handle the exception

  • When defining the method, the throws keyword can be used by the programmer at his own choice. If used, it means that the exception will not be handled when the method is defined.

  • If the main party (main) also uses the throws keyword, it means that the main method does not handle the exception, and finally leaves it to the jvm. The jvm handles the exception by terminating the program

  • throws throws a subclass exception. The caller who calls this method does not have to handle the exception

Note:

throw keyword: You can customize exceptions, that is, throwing exceptions. When thrown, it is an object of the exception class

5. The difference between throws and throw

throws:

  • Used after the method declaration, followed by the exception class name

  • Can be followed by multiple exception classes, separated by commas

  • Indicates that an exception is thrown, and the caller of this method handles it

  • throws indicates the possibility of an exception, but it does not necessarily occur

throw:

  • Used in the method body, followed by the exception object

  • Only one exception object can be thrown

  • means throwing an exception, and there are statements in the method body to handle it

  • throw throwing an exception will definitely happen, it is not a possibility

Code representation:

package com.test;
public class Throw {
    //throws Exception 代表该方法出现异常时不做处理
    public void deposit(int num1,int num2) throws Exception
    {
        // RuntimeException()是运行时异常最大的父类
        int sum;
        if(num2==0){
            throw new RuntimeException("可以自定义一个异常抛出:除数不能为0");
        }else{
           sum=num1/num2;
        }
    }
}
Copy after login

The above is the detailed content of Methods and steps for analyzing Java exception Exception instances. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template