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)
Ale*_*uin 35
以下代码按预期工作(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)
Dart 代码可以抛出和捕获异常。与 Java 不同的是,Dart 的所有异常都是未经检查的异常。方法不声明它们可能抛出哪些异常,并且您不需要捕获任何异常。
Dart 提供Exception和Error类型,但您可以抛出任何非空对象:
throw Exception('Something bad happened.');
throw 'Waaaaaaah!';
Run Code Online (Sandbox Code Playgroud)
使用try,on以及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)
您还可以创建一个抽象异常。
灵感来自于TimeoutException包(阅读Dart API和Dart 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)
| 归档时间: |
|
| 查看次数: |
9874 次 |
| 最近记录: |