java study notes 7-exception handling

1. Basic concepts

       Exception handling is a mechanism in a programming language or computer hardware that is used to handle abnormal conditions in software or information systems (that is, some special conditions beyond the normal execution flow of the program).
       The exception handling (also known as error handling) function provides a way to handle any unexpected or abnormal situations that occur while the program is running. Exception handling uses try, catch, and finally keywords to try operations that may not succeed, handle failures, and clean up resources after the fact.

2. Anomaly classification

  • Error: Internal error of the jvm system, resource exhaustion and other serious situations;
  • Exception: a situation caused by programming errors or accidental external factors;
    Insert picture description here

3. Exception handling method

Through the exception handling mechanism, it can be divided into:

  • Catch exception: try… catch… catch…… finally;
  • Throw an exception: show; shows;

4. Detailed processing method

Catch exception

(1) Syntax:
              try {

                            … // possible exceptions

              }catch{

              }finally{//Unconditionally executed statement;

              }

(2) The role of the grammar module:

       Try statement : The first step to catch an exception is to use a try{...} statement block to select the scope of the exception to be caught, and place the code that may be abnormal in the try block.

       Catch statement : In the catch statement block is the code for processing the exception object. Each try statement block can be accompanied by one or more catch statements to deal with the different types of abnormal objects that may be generated.

       finally statement: (may not be in the program) executed in the last step in catching the exception, regardless of whether an exception occurs in the try or catch code block, the statement in the finally block will be executed;

(3) Case:

//案例一:
public class Erron1 {
    
    
	public static void main(String[] args) {
    
    
		int x=0;
		try {
    
    
			System.out.println(2/x);
		} catch (Exception e) {
    
    //当不知道捕获的是什么类型的异常时,可以直接使用所有异常的父类Exception
			e.printStackTrace();//抛出错误类型,原因,地点
			System.out.println(e.getMessage());//输出错误原因
		}
	}

}
//案例二:
//在try中只要遇到错误就会中断后的语句执行,直接执行catch中的语句;
public class Erron1 {
    
    
	public static void main(String[] args) {
    
    
		int x=0;
		try {
    
    
			System.out.println(1);
			System.out.println(2/x);//捕捉到异常后就会中断后面语句执行catch中语句
			System.out.println(2);
			System.out.println(3/x);
		} catch (Exception e) {
    
    
			System.out.println(3);
			e.printStackTrace();//抛出错误类型,原因
			System.out.println(e.getMessage());//输出错误原因
		}
	}
//案例三:
//1、只要在tyr中有finally语句finally中的程序就会被执行;
//2、在捕获异常的代码块中(try{}里的代码),如果前面的代码有异常了,就不会执行后面的,也不会执行后面代码扑捉的catch
public class Erron1 {
    
    
	public static void main(String[] args) {
    
    
		int x=0;
		String[] arr=new String[]{
    
    "e","c","d"};
		
		try {
    
    
			System.out.println(32/x);
			System.out.println(arr[3]);
		} catch (ArithmeticException e1) {
    
    
			e1.printStackTrace();
			System.out.println(1);
		}catch (ArrayIndexOutOfBoundsException e2) {
    
    
			e2.printStackTrace();
			System.out.println(2);
		}finally {
    
    
			System.out.println("异常处理完成!");
		}
		System.out.println("OK");	
	}
}

Throw an exception

(1) Function: The
       exception is generated, and the caller handles it (the throws clause can be used in the method declaration to declare the list of thrown exceptions. The exception type after throws can be the exception type generated in the method or its parent class )

(2) The difference between throw and throws

  • throw is a statement that throws an exception.
    Syntax: throw (exception object);

  • throws is a statement that a method may throw an exception. (Used when declaring a method, it means that the method may throw an exception)
    Syntax: (modifier) ​​(return value type) (method name) ([parameter list])[throws(exception class)]{...}

(3) Case:

//案例一:throws
public class Error2 {
    
    

public static void main(String[] args) {
    
    

		A a=new A();
		try {
    
           //throws在代码这抛出的异常,在调用方去捕获处理
			a.show();
		} catch (Exception e) {
    
    
			e.printStackTrace();
		}
	}
}

class A{
    
    
	int a=12;
	public void show() throws NullPointerException{
    
    
		A b=null;
		System.out.println(b.a);
	}
}
//案例二:throw
public class Error2 {
    
    

public static void main(String[] args) {
    
    

		B b=new B();
		try {
    
    
			b.age(12);
			b.age(-12);
		} catch (Exception e) {
    
    
			e.printStackTrace();//抛出异常
		}
	}
}

class B{
    
    

	int age;
	public void age(int age) throws Exception {
    
    
		if (age >=0&&age<+120) {
    
    
			this.age=age;
			System.out.println("年龄是:"+this.age);
		}else {
    
    
			throw new Exception("年龄输入有误!!");//人工抛出异常
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_45913017/article/details/112970093