Skip to content

Instantly share code, notes, and snippets.

@kwalrath
Last active August 29, 2015 14:26
Show Gist options
  • Save kwalrath/bb5c060b6d9d050419c8 to your computer and use it in GitHub Desktop.
Save kwalrath/bb5c060b6d9d050419c8 to your computer and use it in GitHub Desktop.
exceptions
class OutOfLlamasException {}
breedMoreLlamas() {}
buyMoreLlamas() {}
cleanLlamaStalls() {}
class Point {}
main() {
var numberOfLlamas = 1;
if (numberOfLlamas <= 0) {
throw new StateError(
'Value must be greater than zero');
}
if (numberOfLlamas <= 0) {
throw 'Out of llamas!';
}
distanceTo(Point other) =>
throw new UnimplementedError();
try {
breedMoreLlamas();
} on OutOfLlamasException {
buyMoreLlamas();
}
try {
breedMoreLlamas();
} on OutOfLlamasException {
// A specific exception
buyMoreLlamas();
} on Exception catch (e) {
// Anything else that is an exception
print('Unknown exception: $e');
} catch (e) {
// No specified type, handles all
print('Something really unknown: $e');
}
try {
breedMoreLlamas();
} finally {
// Always clean up, even if an exception is thrown.
cleanLlamaStalls();
}
try {
breedMoreLlamas();
} catch (e) {
print('Error: $e'); // Handle the exception first.
} finally {
cleanLlamaStalls(); // Then clean up.
}
try {
throw new FormatException('Expected at least 1 section');
} on FormatException catch (e) {
print(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment