exceptions - Creating My Own Exceptions

本文介绍如何在Java中创建自定义异常来表示特定错误情况。通过继承Exception类,可以定义带有构造函数的自定义异常,用于捕获和处理特定类型的错误。示例代码展示了如何抛出并捕获自定义异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Java exception hierarchy can't foresee all the errors we might report, so we can create our own to denote a special problem that your library might encounter.

// exceptions/FullConstructors.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.

class MyException extends Exception {
  MyException() {}

  MyException(String msg) {
    super(msg);
  }
}

public class FullConstructors {
  public static void f() throws MyException {
    System.out.println("Throwing MyException from f()");
    throw new MyException();
  }

  public static void g() throws MyException {
    System.out.println("Throwing MyException from g()");
    throw new MyException("Originated in g()");
  }

  public static void main(String[] args) {
    try {
      f();
    } catch (MyException e) {
      e.printStackTrace(System.out); //  public void printStackTrace(PrintStream s)
    }
    try {
      g();
    } catch (MyException e) {
      e.printStackTrace(System.out);
      // to the standard error stream.
      e.printStackTrace();
    }
  }
}
/* My Output:
Throwing MyException from f()
MyException
        at FullConstructors.f(FullConstructors.java:17)
        at FullConstructors.main(FullConstructors.java:27)
Throwing MyException from g()
MyException: Originated in g()
        at FullConstructors.g(FullConstructors.java:22)
        at FullConstructors.main(FullConstructors.java:32)
MyException: Originated in g()
        at FullConstructors.g(FullConstructors.java:22)
        at FullConstructors.main(FullConstructors.java:32)
*/

PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Two other features are provided as well. Unlike other output streams, a PrintStreamnever throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the checkError method. Optionally, a PrintStream can be created so as to flush automatically; this means that the flush method is automatically invoked after a byte array is written, one of the println methods is invoked, or a newline character or byte ('\n') is written.

All characters printed by a PrintStream are converted into bytes using the platform's default character encoding. The PrintWriter class should be used in situations that require writing characters rather than byte.

references:

1. On Java 8 - Bruce Eckel

2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/exceptions/FullConstructors.java

3. https://docs.oracle.com/javase/8/docs/api/java/lang/Throwable.html#printStackTrace-java.io.PrintWriter-

4. https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html

5. http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/System.java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值