【问题标题】:Exception Class not working异常类不起作用
【发布时间】:2017-04-05 11:09:15
【问题描述】:

我有一个程序正在尝试抛出某个异常。我在这里创建了一个自定义异常类:

import java.io.*;

public class ShapeException extends Exception
{
   public ShapeException(String message)
   {
      super(message);
   }
}

这是我尝试实现异常的类:

import java.io.*;

public class Circle
{      
   private double radius;  

   public Circle(double inRadius )
   {
      if(inRadius < 0.0)
      {
         throw new ShapeException("Shape Exception Occurred...");
      }
      else
      {
         radius = inRadius;
      }
   }

   public double getRadius()
   {
      return radius;
   }

   public void setRadius(double newRadius)
   {
      if(newRadius < 0.0)
      {
         throw new ShapeException("Shape Exception Occurred...");
      }
      else
      {
         radius = newRadius;
      }
   }

   public double area()
   {
      return Math.PI * radius * radius;
   }

   public void stretchBy(double factor )
   {
      if(factor < 0.0)
      {
         throw new ShapeException("Shape Exception Occurred...");
      }
      else
      {
         radius = radius * factor;
      }
   }

   public String toString()
   {
       return "Circle Radius: " + radius; 
   }
}

但是,这不会编译并给我错误,告诉我必须捕获或声明要抛出我的形状异常错误。我究竟做错了什么?这不是声明吗?

【问题讨论】:

标签: java exception io compiler-errors


【解决方案1】:

在java中,每当你抛出一个检查异常时,你需要在方法签名中用throws关键字声明它。下面是没有任何编译错误的代码 sn-p,因为每个方法都有throws 声明,无论ShapeException 被抛出。

public class Circle {
    private double radius;

    public Circle(double inRadius) throws ShapeException {
        if(inRadius < 0.0) {
            throw new ShapeException("Shape Exception Occurred...");
        } else {
            radius = inRadius;
        }
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double newRadius) throws ShapeException {
        if(newRadius < 0.0) {
            throw new ShapeException("Shape Exception Occurred...");
        } else {
            radius = newRadius;
        }
    }

    public double area() {
        return Math.PI * radius * radius;
    }

    public void stretchBy(double factor) throws ShapeException {
        if(factor < 0.0) {
            throw new ShapeException("Shape Exception Occurred...");
        } else {
            radius = radius * factor;
        }
    }

    @Override
    public String toString() {
        return "Circle Radius: " + radius;
    }
}

【讨论】:

    【解决方案2】:

    在 Java 中有两种类型的Exception。您正在使用的是Checked Exception,它用于可恢复的错误。当抛出 Checked Exception 时,您必须处理它。要么你用try-catch块处理它。

    try {
        methodThrowingShapeException()
    } catch (ShapeException e) {
        // Log and handle the Exception
    }
    

    永远不要让 catch 块为空!

    或者您可以将其声明为在方法签名中抛出,在这种情况下,异常必须由方法的调用者处理。

    public void setRadius(double newRadius) throws ShapeException
    

    【讨论】:

      猜你喜欢
      • 2013-10-04
      • 2013-05-15
      • 2016-01-24
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多