Created
January 24, 2020 09:51
-
-
Save AlexVegner/d78380f36e0eaa8d492af6b4a630e5d8 to your computer and use it in GitHub Desktop.
dart_exceptions.dart
This file contains 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
import 'dart:math' as math; | |
class MyError extends Error {} | |
class MyException implements Exception { | |
MyException([dynamic message]); | |
} | |
void checkValue(int value) { | |
switch(value) { | |
case 1: | |
throw FormatException('Expected at least 1 section'); | |
case 2: | |
throw 'Expected at least 1 section'; | |
case 3: | |
throw MyError(); | |
case 4: | |
throw MyException(); | |
} | |
} | |
void main() { | |
final value = math.Random().nextInt(10); | |
try { | |
checkValue(value); | |
print('no exceptions'); | |
} on MyError { | |
print('MyError'); | |
} on MyException catch (e) { | |
print('MyException: $e'); | |
} on FormatException catch (e) { | |
print('FormatException: $e'); | |
} catch (e, s) { | |
print('print error ${e.runtimeType} $s'); // print error with StackTrace | |
rethrow; // Allow callers to see the exception. | |
} finally { | |
print('End!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment