Custom exceptions in dart

别说谁变了你拦得住时间么 提交于 2019-12-23 06:49:52

问题


I have written this code to test how custom exceptions are working in dart.

I'm not getting the desired output could someone explain me how to handle it??

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

}

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

回答1:


You can look at the Exception part of A Tour of the Dart Language.

The following code works as expected (custom exception is been obtained is displayed in console) :

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');
}


来源:https://stackoverflow.com/questions/13579982/custom-exceptions-in-dart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!