Any Java code can throw an exception, such as the code that you write, the code from the Java Development environment package, or the Java Runtime System. No matter who, you can throw an exception through a Java throw statement. Any exception thrown from a method must use the throws clause.
1. Throws throws an exception
If a method may have an exception, but does not have the ability to handle the exception, you can declare the thrown exception with the throws clause at the method declaration. For example, the car may malfunction during operation, the car itself can not handle the fault, so let the driver to deal with.
The throws statement declares the type of exception to throw when the method is defined, and if the exception exception type is thrown, the method is declared to throw all exceptions. Multiple exceptions can be separated with commas. The syntax format for the throws statement is:
MethodName throws Exception1,exception2,.., Exceptionn
{
}
The throws Exception1,exception2,..., Exceptionn after the method name is the list of exceptions to which the declaration is to be thrown. When the method throws an exception from the list of exceptions, the method does not handle the exception of these types and their subclass types, and throws it to the method that invokes the method, which he handles. For example:
Import java.lang.Exception;
public class TestException {
static void Pop () throws negativearraysizeexception {
// Define method and throw Negativearraysizeexception exception
int[] arr = new int[-3];//create array
} public
static void Main (string[) args {//Main method
try {//Try statement handles exception information
pop ();//Call Pop () method
} catch (Negativearraysizeexception e) {
SYSTEM.OUT.P Rintln (Exception thrown by pop () method);//output exception information
}}
}
After the exception is thrown to the caller by using the throws keyword, if the caller does not want to handle the exception, it can continue to throw up, but ultimately there is a caller who can handle the exception.
The pop method does not handle exception negativearraysizeexception, but is handled by the main function.
Throws the rule that throws an exception:
1 if the exception is not checked (unchecked exception), that is, error, runtimeexception or their subclasses, then you can not use the throws keyword to declare the exception to throw, the compilation can still pass smoothly, but at run time will be thrown by the system.
2 must declare any available exceptions (checked exception) that the method can throw. That is, if a method may appear to be subject to a verifiable exception, either capture it with a try-catch statement, or throw it with a throws clause declaration, causing a compilation error
3 only if an exception is thrown, the caller of the method must either process or throw the exception back. When the caller of the method is powerless to handle the exception, it should continue to be thrown instead of being swallowed.
4 The calling method must follow any processing and declaration rules that can be checked for exceptions. If you override a method, you cannot declare exceptions that differ from the override method. Any exception declared must be a class or subclass of the exception that is declared by the overridden method.
For example:
The basis for determining that a method may appear to be abnormal is as follows:
1 There are throw statements in the method. For example, the catch code block for the above Method7 () method has throw statements.
2 calls other methods, and other methods throw an exception with a throws clause declaration. For example, the Method3 () method invokes the Method1 () method, and the Method1 () method declaration throws IOException, so method3 may appear in the IOException () method.
2. Throw an exception using throw
Throw always appears in the body of the function to throw a throwable type of exception. The program terminates immediately after the throw statement, the statement that follows it, and then the try block that contains the catch clause that matches it, in all the try blocks that contain it (possibly in the upper call function).
We know that an exception is an instance object of an exception class, and we can create an instance object of the exception class that is thrown through the throw statement. The syntax format of the statement is:
For example, an exception object that throws a IOException class:
Note that throw can only throw an instance object that can throw a class throwable or its subclasses. The following actions are incorrect:
throw new String ("exception");
This is because string is not a subclass of the Throwable class.
If you throw a check exception, you should also declare the type of exception that the method might throw in the method header. The caller of the method must also check for exceptions thrown by the handle.
If all the methods are thrown on top of each other, the final JVM will handle it, and the process is simply to print exception messages and stack information. If an error or runtimeexception is thrown, the caller of the method can optionally handle the exception.
Package Test;
Import java.lang.Exception; public class TestException {static int quotient (int x, int y) throws MyException {//define method Throw exception if (Y < 0) {// Determine if the parameter is less than 0 throw new myexception ("divisor cannot be negative"); Exception information} return x/y;
return value} public static void Main (String args[]) {//Main method int a = 3;
int b = 0;
The try {//Try statement contains statements that may have an exception: int result = Quotient (a, B);//Call Method quotient ()} catch (MyException e) {//Handle custom exception System.out.println (E.getmessage ());
Output exception information} catch (ArithmeticException e) {//Handle ArithmeticException exception System.out.println ("Divisor cannot be 0");/output hint information catch (Exception e) {//Handle other exception System.out.println ("Other exceptions have occurred");/Output Prompt}} class Myexce Ption extends Exception {//Create custom exception class string message;//define String type variable public myexception (string errormessagr) {//Parent
class method message = ERRORMESSAGR;
The public String getMessage () {//overwrites the GetMessage () method return message; }
}