Java Fundamentals Hardening IO Stream Note 07: Custom exception Overview and custom exception implementations

Source: Internet
Author: User

1. There are often many problems when developing (these exceptions are not provided in the Java Internal system framework)

For example, the test results must be between 0~100.

Obviously Java has no corresponding exception, we need to do an exception ourselves .

(1) Inherit from exception (compile period)

(2) inherit from RuntimeException (running period)

2. Examples of custom exceptions:

(1) Custom exception myexception inherits from exception, then MyException is a compile-time exception, as follows:

1  Packagecom.himi.myexception;2 3 /**4 * Java is unlikely to be considered in all cases, so in real development we may need to define our own exceptions5 * And we casually write a class, is not as an exception class, to think of your class is an exception class, you must inherit from exception or RuntimeException6  * 7 * Two types of methods:8 * A: Inherit exception9 * B: Inherit runtimeexceptionTen  */ One  A  Public classMyExceptionextendsexception{ -  -      Publicmyexception () { the         //TODO Auto-generated constructor stub -     } -      Publicmyexception (String message) { -         Super(message); +     } -      +}

here MyException construction method will not write, look at the source code, see other inherited from the exception of the exception structure is how to write, as follows: timeoutexception

1  Public classTimeoutExceptionextendsException {2     Private Static Final LongSerialversionuid = 1900926677490660714L;3 4     /**5 * Constructs a {@codeTimeoutException} with no specified detail6 * message.7      */8      Publictimeoutexception () {}9 Ten     /** One * Constructs a {@codeTimeoutException} with the specified detail A * message. -      * -      * @paramMessage The detail message the      */ -      Publictimeoutexception (String message) { -         Super(message); -     } +}

Later encountered custom some properties or other will not write, we want to analogy to the Android open source code has been written in the class, view the source, see how others write, according to gourd painting scoop .

(2) Teacher.java as follows:

1  Packagecom.himi.myexception;2 3  Public classTeacher {4      Public voidCheck (intScore)throwsMyException {5         if(score>100 | | score<0) {6             Throw NewMyException ("your input score cannot be greater than 100 or less than 0");//Here the MyException is inherited from exception is the compile-time exception, must be handled, here on the throw 7}Else {8System.out.println ("The score you entered is not a problem");9         }Ten     } One  A}

(3) in compiling a test class, test the logic run, as follows:

1  Packagecom.himi.myexception;2 3 ImportJava.util.Scanner;4 5  Public classStudentdemo {6 7      Public Static voidMain (string[] args) {8Scanner input =NewScanner (system.in);9System.out.println ("Please enter student results:");Ten         intScore =input.nextint (); OneTeacher T =NewTeacher (); A         Try { - T.check (score); -}Catch(MyException e) { the             //TODO Auto-generated catch block - e.printstacktrace (); -         } -  +     } -  +}

The results are as follows:

Java Fundamentals Hardening IO Stream Note 07: Custom exception Overview and custom exception implementations

Related Article

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.