Custom exception,

Source: Internet
Author: User

Custom exception,

// A custom exception must inherit a parent class indicating the exception, because the exception class and the exception object are thrown,
// Only the Throwable system exists. Therefore, only classes and objects in this system can be operated by throws and throws,
// Exception is a subclass of Throwable
Class MyException extends Exception {// custom Exception message Exception (String message) {super (message ); // call the constructor of the parent class} class Demo {int method (int x, int y) throws MyException // throw, declare on the function {judge if (x <0) throw new MyException ("write the exception information shown here") where a custom exception is thrown "); // manually throw a custom exception object through the throw keyword return x/y;} class ExceptionTest {// when throw throws an exception object in the function, you can either use try catch internally or declare it on the function so that the caller can handle public statc void main (String [] args) {Demo demo = new Demo (); try {int x = demo. method (4, 1);} catch (MyException e) {System. out. println (e. toString ());}}}

 

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.