Java类异常未按预期工作

huangapple 未分类评论48阅读模式
英文:

Java class exception not working as intended

问题

我正在尝试简单地使一个类扩展异常类,我已经看过多个相关的视频,代码设置完全相同,但我只是有一个不同的类名。

public class CategoryException extends Exception {

   public CategoryException()
   {
   super("抛出了一个类别异常。");
   }//end method
   
   public CategoryException(String messagePassed)
   {
   super(messagePassed);
   }//end method
   
}

我只想将这个类传递给我的主代码,但是这甚至无法编译,我非常困惑为什么它会说超类没有参数,但在我看到的所有内容中,这就是设置的方式。

CategoryException.java:5: 错误: 无法将类 Exception 中的构造函数应用于给定类型;
   super("抛出了一个类别异常。");
   ^
  需要: 无参数
  找到:    String
  原因: 实际参数列表和形式参数列表的长度不同
CategoryException.java:10: 错误: 无法将类 Exception 中的构造函数应用于给定类型;
   super(messagePassed);
   ^
  需要: 无参数
  找到:    String
  原因: 实际参数列表和形式参数列表的长度不同
2 错误

感谢您的帮助,因为我对如何解决这个问题感到非常困惑,我知道我一定很愚蠢,没有意识到问题出在哪里。


<details>
<summary>英文:</summary>

I&#39;m trying to simply make a class extend the class exception and i&#39;ve seen multiple videos on it with the code setup the exact same way but I just have a different class name

public class CategoryException extends Exception {

public CategoryException()
{
super("A category exception was thrown.");
}//end method

public CategoryException(String messagePassed)
{
super(messagePassed);
}//end method

}

All I want to do is pass this to my main code but this won&#39;t even compile and i&#39;m very confused on why it it is saying that super has has no parameters but in the everything that i&#39;ve seen this is how its supposed to be set up

CategoryException.java:5: error: constructor Exception in class Exception cannot be applied to given types;
super("A category exception was thrown.");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
CategoryException.java:10: error: constructor Exception in class Exception cannot be applied to given types;
super(messagePassed);
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
2 errors

thank you for any help as im very confused on what to do to get by this and I know i must be stupid or something to not realize it.

</details>


# 答案1
**得分**: 0

这可能是你所寻找的:

```java
public class Main {
    public static void main(String[] args) {
        try {
            throw new CategoryException();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

class CategoryException extends Exception {

    public CategoryException() {
        super("已抛出类别异常。");
    }

    public CategoryException(String messagePassed) {
        super(messagePassed);
    }
}

在这个例子中,你可以验证它的工作原理,主方法捕获了你的CategoryException并打印了异常消息 "已抛出类别异常。"。

英文:

This will probably do what you are looking for:

public class Main {
    public static void main(String []args) {
        try {
            throw new CategoryException();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

class CategoryException extends Exception {

    public CategoryException() {
        super(&quot;A category exception was thrown.&quot;);
    }

    public CategoryException(String messagePassed) {
        super(messagePassed);
    }
}

In this case so you can check that it works, the main method just catches your CategoryException and prints out the exception message "A category exception was thrown."

huangapple
  • 本文由 发表于 2020年5月5日 05:27:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/61601937.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定