class Annoyance extends Exception_多态

class Annoyance extends Exception {}
class Sneeze extends Annoyance {}
 
class Human {
 
	public static void main(String[] args) 
		throws Exception {
		try {
			try {
				throw new Sneeze();
			} 
			catch ( Annoyance a ) {
				System.out.println("Caught Annoyance");
				throw a;//抛出一个Annoyance引用
			}
		} 
		catch ( Sneeze s ) {
            //被抓取到Annoyance引用  相当于 a = new  Sneeze();多态(子类对象指向父类引用)
            //父类抛出的异常 子类异常是无法处理的  
			System.out.println("Caught Sneeze");
			return ;
		}
		finally {
			System.out.println("Hello World!");
		}
	}
}
x
 
1
class Annoyance extends Exception {}
2
class Sneeze extends Annoyance {}
3
 
4
class Human {
5
 
6
    public static void main(String[] args) 
7
        throws Exception {
8
        try {
9
            try {
10
                throw new Sneeze();
11
            } 
12
            catch ( Annoyance a ) {
13
                System.out.println("Caught Annoyance");
14
                throw a;//抛出一个Annoyance引用
15
            }
16
        } 
17
        catch ( Sneeze s ) {
18
            //被抓取到Annoyance引用  相当于 a = new  Sneeze();多态(子类对象指向父类引用)
19
            //父类抛出的异常 子类异常是无法处理的  
20
            System.out.println("Caught Sneeze");
21
            return ;
22
        }
23
        finally {
24
            System.out.println("Hello World!");
25
        }
26
    }
27
}
28
输出:
Caught Annoyance 
Caught Sneeze 
Hello World!







猜你喜欢

转载自www.cnblogs.com/mljqqh/p/9694420.html