Dart异常处理无效

时间:2019-01-03 06:46:43

标签: exception dart flutter

我有一个奇怪的问题,我使用了try catch来捕获错误,但是没有用。

void main() {
  testException();
}

void testException() {
  print("start");
  try {
    print("try");
    testThrow();
  } on Exception catch (e) {
    print("catch exceptoin:$e");
  }
  print("end");
}

void testThrow() {
  throw Exception("this is exception");
}

执行结果:

execution result

有没有我没打开的东西?


添加:

经过几次测试,我发现在错误堆栈中,有一个错误:

Not found “dart:isolate/runtime/libisolate_patch.dart”: <source not available>。

我正在尝试将其导入代码中,提示错误如下:

Launching lib/main.dart on iPhone X in debug mode...
Compiler message:
lib/main.dart:6:8: Error: Not found: 'dart:isolate/runtime/libisolate_patch.dart'
import 'dart:isolate/runtime/libisolate_patch.dart';
       ^
Unhandled exception:
Unsupported operation: Cannot extract a file path from a org-dartlang-untranslatable-uri URI
#0      _Uri.toFilePath (dart:core/uri.dart:2617:7)
#1      _writeDepfile (package:vm/frontend_server.dart:682:32)
<asynchronous suspension>
#2      FrontendCompiler.compile (package:vm/frontend_server.dart:363:15)
<asynchronous suspension>
#3      _FlutterFrontendCompiler.compile (package:frontend_server/server.dart:31:22)
<asynchronous suspension>
#4      starter (package:frontend_server/server.dart:133:27)
<asynchronous suspension>
#5      main (file:///b/build/slave/Mac_Engine/build/src/flutter/frontend_server/bin/starter.dart:8:30)
<asynchronous suspension>
#6      _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:287:32)
#7      _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
Compiler failed on /Users/xohome/Documents/shx158/workspace/app/sxapp/lib/main.dart
Error launching application on iPhone X.
Exited (sigterm)

颤抖的医生:

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, v1.0.0, on Mac OS X 10.14 18A391, locale zh-Hans-CN)
[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.io/setup/#android-setup for detailed instructions).
      If Android SDK has been installed to a custom location, set $ANDROID_HOME to that location.
      You may also want to add it to your PATH environment variable.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
[✓] Android Studio (version 3.2)
[✓] IntelliJ IDEA Ultimate Edition (version 2018.1.4)
[✓] VS Code (version 1.30.1)
[✓] Connected device (1 available)

! Doctor found issues in 1 category.

1 个答案:

答案 0 :(得分:0)

图片中第22行的黄色箭头表示调试器在该行暂停。左侧的堆栈跟踪显示了相同的内容:该程序当前处于testThrow方法中。 调试器似乎设置为暂停,因为第22行上没有注册断点。应该有一个名为“ breakpoints”的面板(至少这是英文名称),在“ stack traces”面板下方有两个复选框在左边。顶部的复选框称为“所有异常”,选中该复选框时,调试器将在抛出时中断。

您需要继续执行该程序才能查看稍后发生的情况。您应该可以通过按F5来这样做。 或者,清除复选框,使其在所有抛出时都中断,然后重新启动程序。