首页 > 解决方案 > Apex 自定义异常并使用参数创建新异常

问题描述

我从 Apex 开发人员指南中选择了以下示例。我的问题是,这段throw new OtherException('This is bad');代码是如何工作的。由于没有定义默认/参数化构造函数,我们如何创建一个OtherException带参数的新构造函数?

public class ExceptionExample {
    public virtual class BaseException extends Exception {}
    public class OtherException extends BaseException {}

    public static void testExtendedException() {
        try {
            Integer i=0;
            // Your code here
            if (i < 5) throw new OtherException('This is bad');
        } catch (BaseException e) {  
            // This catches the OtherException
            System.debug(e.getMessage());
        }  
    }
}

Apex 开发人员指南 - 自定义例外

标签: apex

解决方案


推荐阅读