Custom exceptions for Java learning notes

Source: Internet
Author: User

1. Custom Exception classes:

/**@author*/Publicclass extends  Exception {    /*publicfileexception () {            } public    fileexception (String gripe) {        //super (gripe);    } */     Public void Printstacktrace () {        System.out.println ("Suohai");}    }

2. Throw an exception:

classtext{ Public Static voidT ()throwsfileexception{/** Call T (), you must catch this exception, you need to accept this exception in another class, you have to throws Exception * throws Fileexception and other class calls, you must use the Try...catch statement, whether you can Capture, * that this exception has been caught in this method, but not thrown again, the class that calls this method does not catch this exception and still Try...catch*/        /*** You can just throw an exception in a method, catch an exception when calling a method * or you can catch an exception directly within a method-that is, without the Try...catch statement, * Throw the exception directly: throw new Fileexception ("         Hehe "); */        Try{            Throw NewFileexception ();//throws an exception, calling the constructor of the Fileexception class}Catch(fileexception e) {System.out.println (Oh); ThrowE//To catch the exception again outside of the method, it needs to be thrown again, because when the method is called outside, it will always be caught inside the method.         }    }}

3. Catching Exceptions:

 Public classExceptiontest { Public Static voidMain (string[] args) {/**even if this exception is not caught, * you need to use the Try...catch statement **/                Try{text.t (); }         /*catch (Fileexception e) {System.out.println ("aha"); } */        Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }}


4. Operation Result:
Hehe Suohai

Custom exceptions for Java learning notes

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.