Exceptions in JAVA - custom exceptions

/*
 * Because there will be unique problems in the project
 * And these problems are not described by java and encapsulate objects
 * So for these unique problems, you can follow java's idea of ​​encapsulating problems
 * Customized exception encapsulation for unique problems
 *
 * custom exception
 *
 * When the throw exception object appears inside the function, then the corresponding processing action must be given
 * or handle inside try catch
 * Either declare it on the function and let the caller handle it
 *
 * Under normal circumstances, an exception occurs inside the function, and the function needs to be declared
 *
 * It is found that only the exception name is found in the printed result, but there is no exception information
 * Because the custom exception does not define information
 *
 * Custom information
 * Thought that the operation of the exception information has been completed in the parent class
 * So when the subclass is constructed, the exception information is passed to the parent class through the super statement
 * Then you can get the exception information directly through getMessage;
 *
 * Custom exception:
 * Must be a custom class inheriting Exception
 *
 * Inherit Exception reason
 * The exception system has a feature: because both the exception class and the exception object are thrown
 * They are all throwable, which is a unique feature of the Throwable system
 *
 * Only classes and objects in this system can be manipulated by throws and throws
 *
 * The difference between throws and throws
 * throws is used on functions
 * throw is used in the function
 *
 * Throws is followed by the exception class, which can be followed by more than one, separated by commas
 * throw is followed by the exception object
 *
 * */

Here is a small example

/*Requirement: In this unique project, the divisor is a negative number is abnormal */

class FuShuException extends Exception {
	private int value;

	FuShuException(String msg, int value) {
		super(msg);
		this.value = value;
	}

	public int getValue() {
		return value;
	}
}

class Demo2 {
	int div(int a, int b) throws FuShuException {
		if (b < 0)
			throw new FuShuException("The divisor is negative", b); // Manually throw a custom exception object through the throw keyword
		return a / b;
	}
}

public class ExceptionDemo2 {
	public static void main(String args[]) {
		Demo2 d = new Demo2();
		try {
			int x = d.div(4, -1);
			System.out.println("x = " + x);
		} catch (FuShuException e) {
			System.out.println("The wrong negative number is: " + e.getValue());
			System.out.println(e.getMessage());
		}
		System.out.println("over");

	}
}

In this example, the catch is a custom exception. When using an exception, the catch exception must be targeted, so that we can solve the problem in a targeted manner.


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

Guess you like

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