飞镖中的自定义异常

Vic*_*nit 18 exception-handling dart

我编写了这段代码来测试自定义异常如何在dart中工作.

我没有得到所需的输出有人可以解释我如何处理它?

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

}

throwException()
{
  throw new customException('This is my first custom exception');
}
Run Code Online (Sandbox Code Playgroud)

Meh*_*ico 40

如果您不关心 Exception 的类型,则不需要 Exception 类。只需触发这样的异常:

throw ("This is my first general exception");
Run Code Online (Sandbox Code Playgroud)

  • [参考](https://dart.dev/guides/language/language-tour#exceptions)“Dart 程序可以抛出任何非空对象——而不仅仅是 *Exception* 和 Error 对象——作为 **异常* *。” (强调我的),我对您的帖子进行了编辑,因为此处抛出的对象的实际类型是 *String*,您可以通过以下代码片段进行验证: `void main() { try { throw ("Your描述是一个字符串");} catch (e) { print(e.runtimeType); } }` (2认同)

Ale*_*uin 35

你可以看一下Dart语言之旅例外部分.

以下代码按预期工作(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');
}
Run Code Online (Sandbox Code Playgroud)

  • @Vickyonit不从Exception类继承,而是实现它.此外,您可能还需要考虑向该类添加代码. (3认同)
  • 我希望我的异常从Exception类继承。 (2认同)
  • 仅供参考:* Dart程序可以抛出任何非null对象(不仅仅是Exception和Error对象)作为异常* (2认同)
  • 您不想将 Exception 的成员标记为“final”吗? (2认同)

Par*_*iya 8

Dart 代码可以抛出和捕获异常。与 Java 不同的是,Dart 的所有异常都是未经检查的异常。方法不声明它们可能抛出哪些异常,并且您不需要捕获任何异常。

Dart 提供ExceptionError类型,但您可以抛出任何非空对象:

throw Exception('Something bad happened.');
throw 'Waaaaaaah!';
Run Code Online (Sandbox Code Playgroud)

使用tryon以及catch处理异常时的关键字:

try {
  breedMoreLlamas();
} on OutOfLlamasException {
  // A specific exception
  buyMoreLlamas();
} on Exception catch (e) {
  // Anything else that is an exception
  print('Unknown exception: $e');
} catch (e) {
  // No specified type, handles all
  print('Something really unknown: $e');
}
Run Code Online (Sandbox Code Playgroud)

try关键字的作用与在大多数其他语言中一样。使用on关键字按类型过滤特定异常,使用catch关键字获取对异常对象的引用。

如果不能完全处理异常,可以使用rethrow关键字来传播异常:

try {
  breedMoreLlamas();
} catch (e) {
  print('I was just trying to breed llamas!.');
  rethrow;
}
Run Code Online (Sandbox Code Playgroud)

无论是否抛出异常,都要执行代码,请使用finally

try {
  breedMoreLlamas();
} catch (e) {
  // ... handle exception ...
} finally {
  // Always clean up, even if an exception is thrown.
  cleanLlamaStalls();
}
Run Code Online (Sandbox Code Playgroud)

代码示例

tryFunction()下面实施。它应该执行一个不可信的方法,然后执行以下操作:

  • 如果untrustworthy()抛出ExceptionWithMessage,则logger.logException使用异常类型和消息调用(尝试使用onand catch)。
  • 如果untrustworthy()抛出异常,请logger.logException使用异常类型调用(尝试使用on此类型)。
  • 如果untrustworthy()抛出任何其他对象,请不要捕获异常。
  • 一切都被捕获和处理后,调用logger.doneLogging(尝试使用finally)。

.

typedef VoidFunction = void Function();

class ExceptionWithMessage {
  final String message;
  const ExceptionWithMessage(this.message);
}

abstract class Logger {
  void logException(Type t, [String msg]);
  void doneLogging();
}

void tryFunction(VoidFunction untrustworthy, Logger logger) {
  try {
    untrustworthy();
  } on ExceptionWithMessage catch (e) {
    logger.logException(e.runtimeType, e.message);
  } on Exception {
    logger.logException(Exception);
  } finally {
    logger.doneLogging();
Run Code Online (Sandbox Code Playgroud)


Gui*_*lem 5

您还可以创建一个抽象异常。

灵感来自于TimeoutException包(阅读Dart APIDart SDKasync上的代码)。

abstract class IMoviesRepoException implements Exception {
  const IMoviesRepoException([this.message]);

  final String? message;

  @override
  String toString() {
    String result = 'IMoviesRepoExceptionl';
    if (message is String) return '$result: $message';
    return result;
  }
}

class TmdbMoviesRepoException extends IMoviesRepoException {
  const TmdbMoviesRepoException([String? message]) : super(message);
}
Run Code Online (Sandbox Code Playgroud)