gpt4 book ai didi

flutter - 如何创建自定义异常并在 dart 中处理它

转载 作者:行者123 更新时间:2023-12-03 13:22:52 28 4
gpt4 key购买 nike

我编写了这段代码来测试自定义异常在 dart 中的工作方式。
我没有得到所需的输出,有人可以向我解释如何处理吗??

void main() 
{
try
{
throwException();
}
on customException
{
print("custom exception is been obtained");
}

}

throwException()
{
throw new customException('This is my first custom exception');
}

最佳答案

你可以看看Exception part of A Tour of the Dart Language .
以下代码按预期工作(custom exception has been obtained 显示在控制台中):

class CustomException implements Exception {
String cause;
CustomException(this.cause);
}

void main() {
try {
throwException();
} on CustomException {
print("custom exception has been obtained");
}
}

throwException() {
throw new CustomException('This is my first custom exception');
}

关于flutter - 如何创建自定义异常并在 dart 中处理它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13579982/

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