Exceptions in JAVA - Overview

/*
 * Exception: that is, when the program is running abnormally
 * Origin of exception: The problem is also a specific thing in real life, which can also be described in the form of java classes and encapsulated into objects
 * In fact, it is the object embodiment of java after describing the abnormal situation
 *
 * For the division of problems: one is a serious problem, the other is a non-serious problem
 *
 * For serious, java is described by Error class
 * Generally do not write targeted code for Error to describe it
 * For non-serious, java is described by Exception class
 * Exception can be handled in a targeted manner
 *
 * Both Error and Exception have some common content
 * For example: abnormal situation information, cause, etc.
 *
 * Throwable
 * 	|--Error
 * 		
 * 	|--Exception
 *
 * 2. Exception handling
 * java provides unique statements for processing
 * try{
 * The code that needs to be detected;
 * }
 *catch(Exception class variable) {
 * Code for handling exceptions; (processing method)
 * }
 * finally{
 * Statement that will be executed;
 * }
 *
 * 3. Perform common method operations on the caught exception object
 * String getMessage(); get exception information
 *
 * Declare exception on function
 * In order to improve security, let the call out for processing, not to deal with compilation failure
 *
 * Handling of multiple exceptions.
 * 1. When declaring an exception, it is recommended to declare a more specific exception, so that the processing can be more specific
 * 2. If the other party declares several exceptions, there are several catch blocks corresponding to them. Do not define redundant catch blocks.
 * If the exceptions in multiple catch blocks have an inheritance relationship, the parent class exception catch block is placed at the bottom
 *
 * It is recommended that when performing catch processing, the specific processing method must be defined in the catch
 * Don't simply define a sentence e.printStackTrace();
 * Don't simply write an output statement
 *
 *
 * */

class Demo {
	int div(int a, int b) throws ArithmeticException, ArrayIndexOutOfBoundsException { // If the function is declared through the keyword of throws, there may be problems

		int arr[] = new int[a];
		System.out.println(arr[6]);
		return a / b;
	}
}

public class ExceptionDemo {
	public static void main(String args[]) {
		Demo d = new Demo();
		try {
			System.out.println(d.div(14,7));
		} catch (ArithmeticException e) { // Exception e = new
											// ArithmeticException();
			System.out.println("Divisor cannot be 0");
			System.out.println(e.getMessage()); // /by zero
			System.out.println(e.toString()); // exception name: exception information
			e.printStackTrace(); // Exception name: exception information, where the exception occurs
									// In fact, the default exception handling mechanism of the JVM is to call the printStackTrace method
									// print exception stack trace
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("The corner mark is out of bounds");
			System.out.println(e.getMessage());

		}
		System.out.println("over");
	}
}


                                                                                ---------------------By chick

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325593155&siteId=291194637