我現在想顯示帶有自定義例外的自定義錯誤訊息,但我只知道Instance of 'CustomException'
它沒有顯示自定義訊息。
我有以下
try {
batch.set(usersRef, user.toJson());
batch.set(accountsRef, account.toJson());
return await batch.commit();
} on FirebaseException catch (error) {
throw CustomException(
message: 'Future Error createUser',
subMessage: error.message.toString(),
);
}
我的自定義例外類
class CustomException implements Exception {
int? codeNumber;
String? codeString;
String message;
String subMessage;
CustomException({
this.codeNumber,
this.codeString,
required this.message,
required this.subMessage,
});
}
還有我的小部件
}).catchError((error) {
setState(() {
_loader = false;
_errorMessage = error.toString();
});
});
uj5u.com熱心網友回復:
如果您希望顯示自定義訊息,則應覆寫類中的toString()
方法并回傳要在例外中顯示的訊息。CustomException
將此添加到您的CustomException
課程中:
class CustomException implements Exception {
...
@override
String toString() {
return 'Exception: $message ($subMessage)';
}
}
此外,您可以向您的CustomException
類添加公共方法。然后您可以在 CustomException 物件的實體上呼叫該方法來列印訊息:
class CustomException implements Exception {
...
String printStack() {
return 'Exception: $message ($subMessage)';
}
}
然后:
throw CustomException(message: 'Exception title', subMessage: 'Exception description').printStack();
PS:您不需要實作Exception
該類。(如果我錯了,請糾正我。)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/531639.html
標籤:扑
上一篇:顫振顯示對話框
下一篇:如何將小部件引數用作變數?