Custom negative exception Classes class MyException extends Exception {public myexception (String msg)//construction Method {super (MSG);//Invoke the constructor method of the Exception exception class The class Test {public int devide (int x,int y) throws MyException//due to the use of throw in the method body and no try....catch for processing, the throws declaration {if (y<0) throw new MyException ("Exception information: Dividend less than 0.") "); Method uses throw to throw an exception object, and if there is no Try....catch statement in the method to handle the thrown exception, this method should declare an exception throws Xxxexception, which is handled by the caller of the method. if (y==0) throw new MyException ("Exception information: dividend cannot be zero. "); return x/y; } class TestException {public static void main (String [] args) {try {int result=new Test (). Devide (3,1);//int result= New Test (). Devide (3,0); int result=new Test (). Devide (3,1); SYSTEM.OUT.PRINTLN ("The result is" +result); Return catch (MyException e) {System.out.println (E.getmessage ());//system.exit (0);}/*//artthmeticexception arithmetic Operation exception class. For example, divide by 0. catch (ArithmeticException e) {System.out.println ("Exception Information:" +e.getmessage ());//Call the GetMessage method of the Exception class System.out.print (" Exception stack trace: "); E.printstacktrace (); Invoke Exception class Printstacktrace method, print exception Details} * * CATCH (Exception e)//All exceptions previously unhandled are handled by Exception. Because exception is the parent class of all exception classes, this sentence cannot be placed before other statements. Otherwise, there will be errors when compiling. {SYSTEM.OUT.PRINTLN ("Exception Information:" +e.getmessage ());//return;} finally {System.out.println ("The Pragram is running into finally");}//finally statement block. Even though the try and catch statement blocks use the return statement to exit the current method or break out of a loop, the associated finally code block is not affected by execution. Finally, the only thing that cannot be performed by a block of code is system.exit (0)//Each Try statement must have one or more catch statements corresponding to it, the try code block, There can be no other statements between the catch code block and finally code block. System.out.println ("The Pragram is running here!"); When a return or system.exit (0) statement is used in a catch statement, the code here is not executed. } }