简体   繁体   English

Java 异常仅打印消息

[英]Java exception only print message

I'm trying to create an exception class, which I will be throwing on many places in my code, but I don't want it to crash the program everytime it is called.我正在尝试创建一个异常 class,我将在我的代码中的许多地方抛出它,但我不希望它在每次调用它时都使程序崩溃。 I want only some message to be appeared (printed in console).我只想显示一些消息(在控制台中打印)。 This is my class structure so far:到目前为止,这是我的 class 结构:

public class CallException extends Exception {

    CallException(String message) {
        super(message);
        System.out.println(message);
    }
}

Hovewer, everytime I throw CallException , it stops the program completely (with that message).但是,每次我抛出CallException时,它都会完全停止程序(带有该消息)。 I don't want it.我不想要它。 Can you help me or can you see what am I doing wrong?你能帮我还是看看我做错了什么?

if you don't want to exit the program on exceptions, there are 2 ways如果您不想在出现异常时退出程序,有两种方法

1) Use try catch blocks. 1) 使用 try catch 块。 2) Instead of creating exception class, create simple class with static method which prints your message. 2) 不要创建异常 class,而是使用打印您的消息的 static 方法创建简单的 class。

I might have misunderstood your requirements, so feel free to correct me and I can try to help you out that way.我可能误解了您的要求,所以请随时纠正我,我可以尝试以这种方式帮助您。

Good luck and happy learning!祝你好运,学习愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM