Java Fundamentals Hardening IO Stream Note 08: Exception Considerations

Source: Internet
Author: User

1. Exception Precautions:

(1) When a subclass overrides a parent class method, the child class's method must throw the same exception or subclass of the parent exception . (father is broken, son can not be worse than father)

(2) If the parent throws multiple exceptions, the subclass overrides the parent class and can only throw the same exception or a subset of him , and the subclass cannot throw exceptions that the parent class does not have . (father is broken, son can not be worse than father)

(3) If the overridden method does not throw an exception, then the method of the child class is absolutely not to throw an exception , if there is an exception in the subclass method, then the subclass can only try, not throws(father is bad, son can not be worse than father)

2. code example:

1  Packagecom.himi.myexception02;2 3 Importjava.text.ParseException;4 ImportJava.text.SimpleDateFormat;5 Importjava.util.Date;6 7 /**8 * Exception Precautions:9 * A: Subclasses override the parent class method when the subclass's method must throw the same exception or subclass of the parent class exception. (father is broken, son can not be worse than father)Ten * B: If the parent throws multiple exceptions, the subclass overrides the parent class and can only throw the same exception or a subset of his, the subclass cannot throw exceptions that the parent class does not have. (father is broken, son can not be worse than father) One * C: If the overridden method does not throw an exception, then the method of the child class is absolutely not to throw an exception, if there is an exception in the subclass method, then the subclass can only try, not throws (father is bad, son can not be worse than father) A  */ -  Public classExceptiondemo { -  the } -  - classFu { -      Public voidShow ()throwsException { +          -     } +      Public voidmethod () { A          at     } - } -  - classZiextendsFu { -      Public voidShow ()throwsException { -          in     } -      Public voidmethod () { toString s = "2014-11-30"; +SimpleDateFormat SDF =NewSimpleDateFormat (); -Date d =NULL; the         Try { *D =Sdf.parse (s); $}Catch(ParseException e) {Panax Notoginseng             //TODO Auto-generated catch block - e.printstacktrace (); the         } + System.out.println (d); A     } the}

Java Fundamentals Hardening IO Stream Note 08: Exception Considerations

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.