Skip to content

Instantly share code, notes, and snippets.

@BARJ
Last active January 21, 2020 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BARJ/2989349c665a2ee2408bd9004d28a92a to your computer and use it in GitHub Desktop.
Save BARJ/2989349c665a2ee2408bd9004d28a92a to your computer and use it in GitHub Desktop.
Dart Exceptions
/**
* 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