Java advanced programming (Exception)

Keywords: Java Exception

Exception:

An error that occurs during program operation. Exception is an abnormal phenomenon in the running process of the program, resulting in the interruption of the program. In java, various exception phenomena are abstracted to form exception classes
Exceptions are mainly divided into error, general exception and runtime exception

Error: if an error occurs in the application, it cannot be recovered and can only be restarted. The most typical error exception is OutOfMemoryError

General exception (compile time exception):
The processing of this exception must be displayed. If the java program exception is not displayed, the compilation will not pass. The compiler forces ordinary exceptions to be handled by try... catch, or continues to throw them to the upper calling method with the throws declaration. Therefore, ordinary exceptions are also called checkd exceptions.

Runtime exception:
This kind of system exception can be handled or not, so the compiler does not force to handle with try... catch or throw declaration. If run-time exceptions occur, it is generally the programmer's own problem, and robust programs generally do not have such exceptions

Exception handling:
Runtime exceptions do not need preprocessing, and such exceptions can be avoided by standardizing code. Exceptions must be preprocessed during compilation. Otherwise, errors are reported during compilation. There are two preprocessing methods:
1. Capture processing
2. Throw processing

Exception capture and handling:
1. try/catch/finally
try and catch are used to capture and handle exceptions. The specific format is as follows:

   try{
       
   }catch(){
      
   }catch(){//A catch block can have more than one
      
   }finally{
      
   }

Try contains code that may generate exceptions. There can be one or more cathcs after try. catch is the exception to be caught.
When an exception occurs in the try, the code below the exception will not be executed and will jump to the corresponding catch immediately. If there is no exception, it will not jump to the catch. Finally code block means that the code in finally will be executed regardless of whether there is an exception. Finally and catch can be used separately, but finally must be used together with try. For example:

   try{                
   
   }catch(){
  
   }
   
   try{
   
   }finally{
   
   }
package com.scy.exception;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionDemo2 {

	public static void main(String[] args) {
		testException1();
	}
	
	
	//try--catch exception catch 1
	public static void testException1(){
		//try {
			//1. Detect the codes with possible exceptions
			//2. If an exception occurs in a statement in the try code block,
			//Immediately jump to the corresponding catch and try the following code
			//No longer execute
			//3. When multiple detection exceptions occur in the try block, they need to be handled,
			//It can be handled separately through multiple catch clauses.
			
		//}catch(ClassNotFoundException e1){
			//If an exception occurs, type 1 
			//This code block is used for processing. In the development phase, exceptions are generally handled
			//Or print an error message (e1.printStackTrace();)
			//Either output the operation log and save the log file. (logger.error)
			
		//}catch(Exception e2){
			//If exception type 2 is caught
			//There is an inheritance relationship between type 2 and type 1. Catch the child exception first
			//Catch parent exception after.
			//If there is no inheritance relationship, then in order
				
		//}finally {
			//The clause continues to execute regardless of whether an exception occurs
			//Generally, this clause is often used to release system resources.
		//}
		
		int i1=100;
		int i2 = 0;
		try {
			System.out.println("Execution 1");
			int i3 = i1/i2;
			System.out.println("Continue with step 2");
			
		} catch (ArithmeticException e) {
			
			System.out.println("Divisor cannot be 0");
			e.printStackTrace();
			
		}finally {
			System.out.println("Continue with step 3");
		}
		
		
	}
	//try--catch exception catch 2
	public static void testException2(){
		try {
			/*
			 * Exceptions are usually caught in small to large order.
			 * Catch child exceptions first, and then catch parent exceptions
			 */
			FileInputStream fis = 
					new FileInputStream("test01.txt");
			fis.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			
		}catch (IOException e) {
			e.printStackTrace();
		}
		/*} catch (IOException e) {
			e.printStackTrace();
			
		}catch (FileNotFoundException e) {
			e.printStackTrace();
		}*/
	}
}

throws exception capture

In the definition method body, if there are compile time exceptions that need preprocessing, they can be caught or thrown.
When handling exceptions, throw the exception with throws
1. Who calls this method and who is responsible for handling this exception
2. When defining a method, throwing an exception is to remind the user of the method that an exception needs to be preprocessed.
When handling exceptions:
1. Generally, when calling other methods, if there are compile time exceptions in the called method that need preprocessing, select capture processing. Because you call this method, you need to be responsible for processing.
2. When defining a method, if there are compilation exceptions to be handled in the method body, you can choose to catch or throw. If you throw an exception object with the throws statement in the method body, the method should declare the object with throws

public class ExceptionDemo3 {
	public static void main(String[] args) {
		try {
			testException();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

	private static void testException() throws ClassNotFoundException {
		Class.forName("com.mysql.jdbc.Driver");
		
		//1. Register driver
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			System.out.println("Class not found!");
			e.printStackTrace();
		}
	}
}

Posted by fgm on Sun, 26 Sep 2021 12:48:22 -0700