Last active
January 21, 2020 02:49
-
-
Save BARJ/2989349c665a2ee2408bd9004d28a92a to your computer and use it in GitHub Desktop.
Dart Exceptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* DartPad Sharing Guide: https://github.com/dart-lang/dart-pad/wiki/Sharing-Guide | |
* Share: https://dartpad.dev/2989349c665a2ee2408bd9004d28a92a | |
*/ | |
class CustomException implements Exception { | |
final String message; | |
CustomException(this.message); | |
@override | |
String toString() { | |
return "$runtimeType: $message"; | |
} | |
} | |
class SearchException extends CustomException { | |
SearchException(String message) : super(message); | |
} | |
class NormalException implements Exception { | |
final String message; | |
NormalException(this.message); | |
} | |
class NamedConstructorException implements Exception { | |
String message; | |
NamedConstructorException(this.message); | |
NamedConstructorException.fromException(Exception exception) { | |
this.message = exception.toString(); | |
} | |
@override | |
String toString() { | |
return "$runtimeType: $message"; | |
} | |
} | |
void main() { | |
print(CustomException('foo bar').toString()); // CustomException: foo bar | |
print(SearchException('foo bar').toString()); // SearchException: foo bar | |
print(Exception('foo bar').toString()); // Exception: foo bar | |
print(NamedConstructorException('foo bar')); // NamedConstructorException: foo bar | |
print(NamedConstructorException.fromException(Exception('foo bar'))); // NamedConstructorException: Exception: foo bar | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment