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