按一般的重写规则, 重写的方法抛出的异常不能比 被重写的方法抛出的异常泛围更大.package AssertExample;
import java.io.*;
class A extends B
{
public static void main(String[] arg)
{ }
void m1() throws Ex22,Ex1,Ex2
{
}
void m2() throws NumberFormatException,ClassCastException,ArrayIndexOutOfBoundsException
{
}}
class B
{
void m1() throws Ex0
{
}
void m2() throws FileNotFoundException
{
}}class Ex0 extends Exception
{
}
class Ex1 extends Ex0
{
}
class Ex2 extends Ex0
{
}
class Ex22 extends Ex2
{
}类B中的m1()既然抛出了异常Ex0,那么类A中的m1()就只能抛出Ex1,Ex2,Ex22中的某几个,如果抛出ClassCastException等将会编绎出错.
但是大家看看m2(),类A中的m2()抛出的异常明显比B中的m2()抛出的要多,并且都不是FileNotFoundException的子类,但编绎却通过了,这是什么呢?好奇怪啊.
import java.io.*;
class A extends B
{
public static void main(String[] arg)
{ }
void m1() throws Ex22,Ex1,Ex2
{
}
void m2() throws NumberFormatException,ClassCastException,ArrayIndexOutOfBoundsException
{
}}
class B
{
void m1() throws Ex0
{
}
void m2() throws FileNotFoundException
{
}}class Ex0 extends Exception
{
}
class Ex1 extends Ex0
{
}
class Ex2 extends Ex0
{
}
class Ex22 extends Ex2
{
}类B中的m1()既然抛出了异常Ex0,那么类A中的m1()就只能抛出Ex1,Ex2,Ex22中的某几个,如果抛出ClassCastException等将会编绎出错.
但是大家看看m2(),类A中的m2()抛出的异常明显比B中的m2()抛出的要多,并且都不是FileNotFoundException的子类,但编绎却通过了,这是什么呢?好奇怪啊.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货