Dart错误处理的特定错误 [英] Dart Error Handling of specific error
本文介绍了Dart错误处理的特定错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想处理Dart中可能发生的各种错误。
我正在使用try / catch,但想知道如何确定可能发生的不同错误之间。
例如,当没有网络连接时,我会遇到此错误:
I would like to handle different errors that could happening in Dart. I am using try/catch but wondering how to determine between the different errors that can occur. For instance I have this error when there is no network connection:
PlatformException(Error 17020, FIRAuthErrorDomain, Network error (such as timeout, interrupted connection or unreachable host) has occurred.)
用户名/密码不正确:
PlatformException(Error 17009, FIRAuthErrorDomain, The password is invalid or the user does not have a password.)
我想根据发生的错误采取不同的措施。
最好的方法是什么?
更新:使用以下方法结束!
I would like to take different actions depending on the error that occurs. What would be the best approach here? Update: Ended up using the below way!
import 'package:flutter/services.dart' show PlatformException;
try {
//Something!
} on PlatformException catch (e) {
switch (e.code) {
case "Error 17009":
// handle
break;
case "Error 17020":
// handle
break;
case "Error 17011":
//handle
break;
default:
throw new UnimplementedError(e.code);
}
}
推荐答案
I会使用 try
/ catch
和 switch
/ case
:
I'd use a try
/catch
and a switch
/case
:
import 'package:flutter/services.dart' show PlatformException;
try {
...
} on PlatformException catch(e) {
switch(e.code) {
case '17009':
// handle
break;
case '17020':
// handle
break;
default:
throw new UnimplementedError(error.code);
}
}
这篇关于Dart错误处理的特定错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文