java learning ~ exception try catch, throw, finally

 

Thorwable class (indicating that it can be thrown) is the superclass of all exceptions and errors. The two direct subclasses are Error and Exception, which represent errors and exceptions respectively.

The Exception type Exception is divided into runtime Exception and non runtime Exception, which are very different, also known as Unchecked Exception and Checked Exception.

1. Error and Exception

Error is an error that cannot be handled by the program. It is generated and thrown by the JVM, such as OutOfMemoryError, ThreadDeath, etc. When these exceptions occur, the Java virtual machine (JVM) will generally choose thread termination.

Exception s are exceptions that can be handled by the program itself. These exceptions are divided into two categories: runtime exceptions and non runtime exceptions. The program should handle these exceptions as much as possible.

2. Runtime and non runtime exceptions

Runtime exceptions are RuntimeException class and its subclasses, such as NullPointerException, IndexOutOfBoundsException, etc. these exceptions are not checked, and the program can choose to capture and handle them or not. These exceptions are generally caused by program logic errors. The program should avoid such exceptions as much as possible from the perspective of logic.

Non runtime exceptions are exceptions other than RuntimeException, which belong to Exception class and its subclasses in type. From the perspective of program syntax, it is an Exception that must be handled. If it is not handled, the program cannot be compiled. Such as IOException, SQLException, and user-defined Exception exceptions. Generally, exceptions are not checked by users.

 

try---catch: if there is an error in the code operation, you can catch it, but it cannot affect the subsequent code operation

Example: numeric string to integer type. Strings with letters cannot be converted logically, but there is no problem in syntax

//throw catch
public class Test {
	public static void main(String[] args) {
		int a1=change("234");
		int a2=change("123");
		int a3=change("dlakds456");//An error message interrupts the entire thread
      	System.out.println("hello everyone");//If the thread above is interrupted, this sentence will not be executed
	
	}
	
	public static Integer change(String x) {
		return Integer.parseInt(x);//Convert x to integer type
	}
}
public class Test {
	public static void main(String[] args) {
		//try catch when you are reminded that an error may be reported
		try {
			int a1=change("234");
			int a2=change("123");
			int a3=change("dlakds456");//Error reporting terminal entire thread
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("There are letters in the string, which cannot be converted");
		}
	
	}
	
	// throws Exception reminder, possible error
	public static Integer change(String x) throws Exception{
		return Integer.parseInt(x);//Convert x to integer type
	}
}

throws is passed to remind this method of possible errors and pass the problem to the caller

public class Test2 {
	public static void main(String[] args) {
		m2();
	}
	
	public static void m1() {
		
	}
	public static void m2() {
			try {
				m3();
			} catch (Exception e) {
				// e.getMessage() is shared within the thread
				System.out.println(e.getMessage()+"cover m2 Solved");
			}
		}
	public static void m3() throws Exception {
		m4();
	}
	public static void m4() throws Exception {
		m5();
	}
	public static void m5() throws Exception{
		throw new Exception("I made a mistake");//The method passed to the first catch
	}
}

throws:

It is used to declare all exceptions that may be generated by a method. Instead of doing any processing, it uploads the exceptions to whoever calls me.
Used after the method declaration, followed by the exception class name
It can be separated from multiple exception class names by commas
Indicates that an exception is thrown and handled by the caller of the method
throws indicates a possibility of exceptions that are not certain to occur

 

throw throws an exception type

public class Test{
    public static void main(String args[]){
        try{
            throw new Exception("Throw it yourself.") ;    // An instantiated object that throws an exception
        }catch(Exception e){
            System.out.println(e) ;
        }
    }
};


throw:

Is used to throw a specific exception type.
When used in a method body, it is followed by the exception object name. Only one exception object name can be thrown
Indicates that an exception is thrown and handled by the statement in the method body
Throw means that an exception is thrown, and executing throw must throw some kind of exception
 

The code area that can always be executed in the finally exception generally executes the io operation to close

try{ 
 return //return finall will also be executed
      }catch{
      }finally{  }//It will eventually be implemented,

Running order: the return statement will be executed first

public class Test3 {
	public static void main(String[] args) {
	m1();
	}
	
	public static int m1() {
		try {
			return m2();//Although it is written in one line, m2(); finally; return then
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			System.out.println("888");
		}
		System.out.println("555");
		return 1;
	}
	public static int m2() {
		System.out.println("6666");
		return 3;
	
	}
	
}

finally ,final ,finalize

The code area that can always be executed in the finally exception generally executes io operation to close (interview)

final is the keyword used to define constants

finalize() garbage collection. This method is a system call and does not need to be called manually by programmers

Keywords: Java

Added by esfisher on Mon, 07 Mar 2022 06:27:37 +0200