【问题标题】:to create our own custom exception, throw or throws or try & catch? [duplicate]创建我们自己的自定义异常,throw 或 throws 还是 try & catch? [复制]
【发布时间】:2018-07-28 17:34:51
【问题描述】:

在 Java 中如果我们想创建自己的自定义异常,我们应该使用哪个关键字?

投掷还是投掷还是尝试接住?

【问题讨论】:

  • 你已经尝试了什么?您阅读了有关该主题的哪些文档?
  • class Something extends Exception { }
  • 在您发明自己的自定义异常之前,首先要彻底了解异常概念(您现在显然没有)。否则你的代码将成为维护的噩梦。

标签: java try-catch throw throws


【解决方案1】:

您需要创建一个扩展类 Exception 的新类。 throws 关键字用于指示当前类传播异常, throw 关键字用于启动传播,try catch 确定捕获异常的块。

 public class MyClass extend Exception {
         public MyClass(String msg)
             super(msg);
}

【讨论】:

  • 谢谢你的回答。真的很有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-05
  • 2021-09-08
  • 2014-08-23
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
  • 2013-01-26
相关资源
最近更新 更多