我已编写此代码来测试自定义异常在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 is been obtained
:
class CustomException implements Exception {
String cause;
CustomException(this.cause);
}
void main() {
try {
throwException();
} on CustomException {
print("custom exception is been obtained");
}
}
throwException() {
throw new CustomException('This is my first custom exception');
}
如果您不关心Exception的类型(和类型处理),则不需要Exception类。只是触发一个异常:
throw ("This is my first general exception");