Skip to content

Instantly share code, notes, and snippets.

@fvisticot
Last active October 3, 2019 22:20
Show Gist options
  • Save fvisticot/c1523398f3fa0a1f5a312a39e7567b6c to your computer and use it in GitHub Desktop.
Save fvisticot/c1523398f3fa0a1f5a312a39e7567b6c to your computer and use it in GitHub Desktop.
Exceptions
void main() {
int res = ComputeService().add(2, 4);
print("Res: $res");
try {
int res = ComputeService().add(null, 4);
print("Res: $res");
} on ComputeServiceException catch (e){
print("Error: ${e.cause}");
}
try {
int res = SuperComputeService().add(3, 5);
print("Res: $res");
} catch (e){
print("Error: ${e.cause}");
}
res = ComputeService().substract(val1: 3);
print("Res: $res");
}
class ComputeService {
int substract({int val1, int val2=1}) {
return val1 - val2;
}
int add(int a, int b) {
if (a == null) {
throw ComputeServiceException("parameter a can not be null");
}
return a + b;
}
}
class SuperComputeService implements ComputeService {
int add(int a, int b) {
if (a == null) {
throw ComputeServiceException("parameter a can not be null");
}
return a + b;
}
int substract({int val1, int val2=1}) {
return val1 - val2;
}
}
class ComputeServiceException implements Exception {
String cause;
ComputeServiceException(this.cause);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment