package com.eric.exception;
/**
* 本类的目的主要包括4点
* (1)try 后面可以没有catch而是只有finally code block[noCatchTryBolck method]
* (2)if inherit class override method from
* super class and need to throw exception, that's exception must compatible with parent class method exception[test1,test2]
* 主要原因是因为如果某个方法是用了父类作为参数,而且捕获了其方法抛出的异常,如果子类方法的异常范围大于或者是和其不兼容, 则将父类作为参数的方法将不能正确处理子类所抛出的异常,所以java强制这一特性
*
* (3)INHERITCLASS.method.limit>=SUPERCLASS.method.limit(for override)[test3]
* 如果子类的覆盖方法的访问权限比超类的访问权限低,则有可能在可以访问超类方法的地方却不能访问到子类的方法,所以这一点需要强制执行 例如 超类的方法a的权限是public
* 而子类覆盖了其方法而且将权限设置为protected,则如果某个包外部方法调用了用超类作为参数,则此时在执行的时候便会访问不到子类的a方法
* (4)对于静态内部类的非静态方法的方法方式
* new ExceptionFeatures.InheritClass().test1();
*
*
* */
public class ExceptionFeatures {
public static final String PCE_VERSION_CONTROL = "@(#) $RCSfile: $, $Revision: $, $Date: {1}quot;;
public static void main(String[] args) {
catchTryBolck();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>");
try {
exceptionWidely();
} catch (ParentException e) {
e.printStackTrace(System.out);
}
System.out.println(">>>>>>>>>>>>>>>>>>>>");
noCatchTryBolck();
}
/**
* 在try的结构中可以不包含catch语句
* */
static void noCatchTryBolck() {
try {
int a = 12 / 0;
} finally {
System.out.println("div by zero!");
}
System.out.println("out of finally");
}
/**
* finally 语句块一定会被执行
* */
static void catchTryBolck() {
try {
int a = 12 / 0;
} catch (ArithmeticException ex) {
ex.printStackTrace(System.out);
} finally {
System.out.println("div by zero!");
}
System.out.println("out of finally");
}
/**
* 子类的exception的范围不能比父类更广
* @throws ParentException
* */
static void exceptionWidely() throws ParentException {
new ExceptionFeatures.InheritClass().test1();
new ExceptionFeatures.InheritClass().test2();
new ExceptionFeatures.InheritClass().test3();
}
private static class ParentException extends Exception {
ParentException() {
}
ParentException(String msg) {
super(msg);
}
}
private static class InheritException extends ParentException {
InheritException() {
}
InheritException(String msg) {
super(msg);
}
}
private static class InheritException2 extends ParentException {
InheritException2() {
}
InheritException2(String msg) {
super(msg);
}
}
private static class Parent {
public void test1() throws ParentException {
System.out.println("in parent.test1()");
throw new ParentException();
}
public void test2() throws InheritException {
System.out.println("in parent.test2()");
throw new InheritException();
}
public void test3() {
System.out.println("in parent.test3()");
}
}
private static class InheritClass extends Parent {
/**
* - overrides com.eric.exception.ExceptionFeatures.Parent.test1 - Exception InheritException2 is not compatible
* with throws clause in ExceptionFeatures.Parent.test1()
* */
// public void test2() throws InheritException2 {
// System.out.println("in InheritClass.test2()");
// throw new InheritException2();
// }
/**
* - overrides com.eric.exception.ExceptionFeatures.Parent.test1 - Exception InheritException2 is not compatible
* with throws clause in ExceptionFeatures.Parent.test1()
* */
// public void test2() throws ParentException {
// System.out.println("in InheritClass.test2()");
// throw new ParentException();
// }
public void test2() throws InheritException {
System.out.println("in InheritClass.test2()");
throw new InheritException();
}
/**
* 在父类的test1方法是抛出ParentException异常,由于InheritException是ParentException的子类,说以在InheritClass类中的test1方法是可以编译通过的
* */
public void test1() throws InheritException {
System.out.println("in InheritClass.test1()");
throw new InheritException();
}
/**
* - Cannot reduce the visibility of the inherited method from ExceptionFeatures.Parent
*
* */
// @Override
// protected void test3(){
//
// }
@Override
public void test3() {
System.out.println("in inherit class test3() method output");
}
}
}
/*
*
* History:
*
*
*
* $Log: $
*/