I am learning the Exception handling in java (basically in inheritance)

前端 未结 4 953
刺人心
刺人心 2021-01-23 03:15

just look at program below..

import java.io.*;
import java.rmi.*;
class class1
{
  public void m1() throws RemoteException 
{
  System.out.println(\"m1 in class1         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-23 03:39

    When overriding a method that throws an Exception you can only throw the same Exception or a more specific subtype of that Exception.

    RemoteException is a subtype of IOException. Therefore when the parent method throws IOException your child method can throw RemoteException.

    You can also think it through logically. If a child method threw a broader Exception than the parent method, then the method might throw an Exception that doesn't match the parent.

提交回复
热议问题