Skip to content

Instantly share code, notes, and snippets.

@developerjamiu
Created May 3, 2022 12:19
Show Gist options
  • Save developerjamiu/ae353c421fc826ee50cad8384f6c7c24 to your computer and use it in GitHub Desktop.
Save developerjamiu/ae353c421fc826ee50cad8384f6c7c24 to your computer and use it in GitHub Desktop.
void main() {}
/// A piece of code checking for errors using exceptions
/// Checking using exceptions
void checkingForExceptions() {
try {
/// performing operation
} on FormatException {
/// Handle exception
print('Handling format exception');
} on StateError {
/// Handle exception
print('Handling state error');
/// You can keep adding more chains of On to handle specific errors
/// Then use catch to catch all other excections
} catch (ex) {
print(ex.toString());
}
}
/// Checking using control flow
void checkingUsingIfElse() {
// assume
final bool ifSuccessful = performComplexOperation();
if (!ifSuccessful) {
print('An error occured');
}
}
/// Should return true if operation is successful, otherwise return false
/// We will return false to simulate an error
bool performComplexOperation() {
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment