子类覆写父类的方法时,如果父类的方法进行了异常声明了,子类可以不理会这个声明,不需要进行异常声明。

None.gifpackage com.vitamin.Console;
None.gif
import java.lang.Throwable;
None.gif
None.gif
None.gif
public class exceptionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
InBlock.gif    
public static void main(String[] args) throws Exception
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// TODO Auto-generated method stub]
InBlock.gif
        derive d = new derive();
InBlock.gif        d.process();
InBlock.gif    
ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
class myException extends Exception
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    myException()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
super();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
class base
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public base()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void process() throws myException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"process() in base class");
InBlock.gif        
throw new myException();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
class derive extends base
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public derive()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
super();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public void process()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        System.out.println(
"process() in derived class");
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

2.如果你先用父类catch掉了异常,那子类异常的catch块就不能到达,编译器就会报错:

None.gifpackage com.vitamin.Console;
None.gif
import java.lang.Throwable;
None.gif
None.gif
None.gif
public class exceptionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
InBlock.gif    
public static void main(String[] args) throws Exception
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
// TODO Auto-generated method stub]
InBlock.gif
        try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new exception2();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(myException ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.err.println(ex.getMessage().toString());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch(exception2 ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.err.println(ex.getMessage().toString());
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
class myException extends Exception
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    myException()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
super();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
class exception2 extends myException
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public exception2()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
super();
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

   这种用法编译器是会报错的,应该用防止父类屏蔽掉子类异常处理。