我编写了这段代码来测试自定义异常在 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/