Created
May 3, 2022 12:19
-
-
Save developerjamiu/ae353c421fc826ee50cad8384f6c7c24 to your computer and use it in GitHub Desktop.
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
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