Java. Exception: Custom exception class, Throw,throws,try...catch,finally__java

Source: Internet
Author: User
Tags getmessage stack trace

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. } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.