gpt4 book ai didi

Java - 如何编写自己的异常来查找特定错误

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:54 25 4
gpt4 key购买 nike

我在这里看到了其他问题,询问如何实现自己的异常,但没有指定如何检查异常。我的意思是,例如,我希望当输入的数字是数字 10 时发生异常。我如何编写自己的异常来检查数字是否为 10,如果是则抛出异常。

提前致谢!

最佳答案

在代码中抛出自定义异常:

if(input == 10){ throw new WrongNumberException("You have entered 10");}

创建自定义Exception类:

class WrongNumberException extends Exception{

public WrongNumberException()
{
super();
}

public WrongNumberException(String message)
{
super(message);
}
}

关于Java - 如何编写自己的异常来查找特定错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26921281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com