java子类的子类怎么定义_用户定义的Java异常子类

java子类的子类怎么定义

Java provides rich set of built-in exception classes like: ArithmeticException, IOException, NullPointerException etc. all are available in the java.lang package and used in exception handling. These exceptions are already set to trigger on pre-defined conditions such as when you divide a number by zero it triggers ArithmeticException.

Java提供了丰富的内置异常类集,例如:ArithmeticException,IOException,NullPointerException等。所有这些都可以在java.lang包中使用,并用于异常处理。 这些异常已经设置为在预定义条件下触发,例如,将数字除以零时会触发ArithmeticException。

Apart from these classes, Java allows us to create our own exception class to provide own exception implementation. These type of exceptions are called user-defined exceptions or custom exceptions.

除了这些类之外,Java还允许我们创建自己的异常类以提供自己的异常实现。 这些类型的异常称为用户定义的异常或自定义异常。

You can create your own exception simply by extending java Exception class. You can define a constructor for your Exception (not compulsory) and you can override the toString() function to display your customized message on catch. Lets see an example.

您可以通过扩展java Exception类来创建自己的异常。 您可以为Exception(不是强制性的)定义一个构造函数,并且可以重写toString()函数以在捕获时显示您的自定义消息。 让我们来看一个例子。

示例:自定义异常 (Example: Custom Exception)

In this example, we are creating an exception class MyException that extends the Java Exception class and

在此示例中,我们将创建一个异常类MyException,该类扩展了Java Exception类,并且

class MyException extends Exception
{
  private int ex;
  MyException(int a)
  {
    ex = a;
  }
  public String toString()
  {
    return "MyException[" + ex +"] is less than zero";
  }
}

class Demo
{
  static void sum(int a,int b) throws MyException
  {
    if(a<0)
    {
      throw new MyException(a); //calling constructor of user-defined exception class
    }
    else
    {
      System.out.println(a+b);
    }
  }

  public static void main(String[] args)
  {
    try
    {
      sum(-10, 10);
    }
    catch(MyException me)
    {
      System.out.println(me); //it calls the toString() method of user-defined Exception
    }
  }
}

MyException[-10] is less than zero

MyException [-10]小于零

示例:自定义异常 (Example: Custom Exception)

Lets take one more example to understand the custom exception. Here we created a class ItemNotFound that extends the Exception class and helps to generate our own exception implementation.

让我们再举一个例子来了解自定义异常。 在这里,我们创建了一个ItemNotFound类,该类扩展了Exception类并有助于生成我们自己的异常实现。

class ItemNotFound extends Exception
{
  public ItemNotFound(String s) {
    super(s);
  }

}

class Demo
{
  static void find(int arr[], int item) throws ItemNotFound
  {
    boolean flag = false;
    for (int i = 0; i < arr.length; i++) {
      if(item == arr[i])
        flag = true;
    }
    if(!flag)
    {
      throw new ItemNotFound("Item Not Found"); //calling constructor of user-defined exception class
    }
    else
    {
      System.out.println("Item Found");
    }
  }

  public static void main(String[] args)
  {
    try
    {
      find(new int[]{12,25,45}, 10);
    }
    catch(ItemNotFound i)
    {
      System.out.println(i);
    }
  }
}

ItemNotFound: Item Not Found

ItemNotFound:找不到项目

要记住的要点 (Points to Remember)

  1. Extend the Exception class to create your own exception class.

    扩展Exception类以创建自己的异常类。

  2. You don't have to implement anything inside it, no methods are required.

    您无需在其中实现任何东西,不需要任何方法。

  3. You can have a Constructor if you want.

    如果需要,可以有一个构造函数。

  4. You can override the toString() function, to display customized message.

    您可以覆盖toString()函数以显示自定义消息。

翻译自: https://www.studytonight.com/java/create-your-own-exception.php

java子类的子类怎么定义

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值