java处理异常的方法有_带有异常处理的Java方法覆盖

java处理异常的方法有

There are few things to remember when overriding a method with exception handling because method of super class may have exception declared. In this scenario, there can be possible two cases: either method of super class can declare exception or not.

当用异常处理覆盖方法时,要记住的事情很少,因为超类的方法可能声明了异常。 在这种情况下,可能存在两种情况:超类的任何一种方法都可以声明异常或不声明异常。

If super class method declared exception then the method of sub class may declare same exception class, sub exception class or no exception but can not parent of exception class.

如果超类方法声明了异常,则子类的方法可以声明相同的异常类,子异常类或不声明异常,但不能声明异常类的父类。

For example, if a method of super class declared ArithmeticException then method of subclass may declare ArithmeticException, its subclass or no exception but cannot declare its super/parent class like: Exception class.

例如,如果超类的方法声明为ArithmeticException,则子类的方法可以声明ArithmeticException,其子类或不声明任何异常,但不能声明其父/父类,例如:Exception类。

In other scenario, if super class method does not declare any exception, then sub class overriden method cannot declare checked exception but it can declare unchecked exceptions. Lets see an example.

在其他情况下,如果超类方法未声明任何异常,则子类重写方法无法声明已检查的异常,但可以声明未检查的异常。 让我们来看一个例子。

具有检查异常的子类中的方法重写 (Method overriding in Subclass with Checked Exception)

Checked exception is the exception which is expected or known to occur at compile time hence they must be handled at compile time.

检查的异常是在编译时预期或已知发生的异常,因此必须在编译时进行处理。

import java.io.*;

class Super
{
  void show() { 
    System.out.println("parent class"); 
  }
}

public class Sub extends Super
{
  void show() throws IOException                //Compile time error
  { 
    System.out.println("parent class"); 
  }

  public static void main(String[] args)
  {
    Super s=new Sub();
    s.show();
  }
}

In the example above, the method show() doesn't throw any exception when its declared/defined in the Super class, hence its overridden version in the class Sub also cannot throw any checked exception.

在上面的示例中, show()方法在Super类中声明/定义时不会引发任何异常,因此,其在Sub类中的重写版本也不会引发任何检查的异常。

If we try to do so, we will get a compile time error.

如果我们尝试这样做,将会得到一个编译时错误。

子类中具有UnChecked异常的方法重写 (Method overriding in Subclass with UnChecked Exception)

Unchecked Exceptions are the exception which extend the class RuntimeExeption and are thrown as a result of some runtime error.

未检查的异常是扩展类RuntimeExeption的异常,由于某些运行时错误而引发。

import java.io.*;

class Super
{
  void show() { 
    System.out.println("parent class"); 
  }
}

class Sub extends Super
{
  void show() throws ArrayIndexOutOfBoundsException
  { 
    System.out.println("child class"); 
  }

  public static void main(String[] args)
  {
    Super s = new Sub();
    s.show();
  }
}

child class

儿童班

Because ArrayIndexOutOfBoundsException is an unchecked exception hence, overrided show() method can throw it.

由于ArrayIndexOutOfBoundsException是未经检查的异常,因此,覆盖的show()方法可以将其抛出。

有关覆盖方法和异常的更多信息 (More about Overriden Methods and Exceptions)

If Super class method throws an exception, then Subclass overriden method can throw the same exception or no exception, but must not throw parent exception of the exception thrown by Super class method.

如果Super类方法引发异常,则Subclass重写方法可以引发相同异常或不引发异常,但不能引发Super类方法引发的异常的父异常。

It means, if Super class method throws object of NullPointerException class, then Subclass method can either throw same exception, or can throw no exception, but it can never throw object of Exception class (parent of NullPointerException class).

这意味着,如果Super类方法抛出NullPointerException类的对象,则Subclass方法可以抛出相同的异常,也可以不抛出任何异常,但它永远不会抛出Exception类的对象(NullPointerException类的父对象)。

具有相同异常的子类重写方法的示例 (Example of Subclass overriden method with same Exception)

Method of a sub class can declare same exception as declared in the super class. See the below example.

子类的方法可以声明与超类中声明的异常相同的异常。 请参见以下示例。

import java.io.*;
class Super
{
 void show() throws Exception
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception           //Correct
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }
}

child class

儿童班

无异常的子类重写方法的示例 (Example of Subclass overriden method with no Exception)

It is optional to declare the exception in sub class during overriding. If method of super class declared an exception then it is upto the subclass to declare exception or not. See the below example.

在重写期间,可以在子类中声明异常。 如果超类的方法声明了异常,则取决于子类是否声明异常。 请参见以下示例。

import java.io.*;
class Super
{
 void show() throws Exception
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show()                            //Correct
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }
}

child class

儿童班

具有父异常的子类重写方法的示例 (Example of Subclass overriden method with parent Exception)

It is not allowed to declare parent class exception in the subclass method, we get compile time error if we try to compile that program. See the below example.

不允许在子类方法中声明父类异常,如果尝试编译该程序,则会出现编译时错误。 请参见以下示例。

import java.io.*;
class Super
{
 void show() throws ArithmeticException
  {  System.out.println("parent class");  }
}

public class Sub extends Super {
 void show() throws Exception                   //Compile time Error
   { System.out.println("child class"); }

 public static void main(String[] args)
 {
  try {
   Super s=new Sub();
   s.show();
   }
  catch(Exception e){}
 }
}

Compile time error

编译时间错误

翻译自: https://www.studytonight.com/java/methodoverriding-with-exception-handling.php

java处理异常的方法有

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值