异常处理
throw
bool flag = false;
if (flag == false) {
throw ('this is a wrong tips');
}
/*
Unhandled exception:
this is a wrong tips
#0 main (file:///d:/Flutter/dart_test/dart/dart3.dart:4:5)
#1 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#2 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
*/
try-catch
try {
breedMoreLlamas();
} on OutOfLlamasException {
// 一个特殊的异常
buyMoreLlamas();
} on Exception catch (e) {
// 其他任何异常
print('Unknown exception: $e');
} catch (e) {
// 没有指定的类型,处理所有异常
print('Something really unknown: $e');
}
finally
//任何匹配的 catch 执行完成后,再执行 finally
try {
breedMoreLlamas();
} catch (e) {
print('Error: $e'); // Handle the exception first.
} finally {
cleanLlamaStalls(); // Then clean up.
}