Last active
October 3, 2019 22:20
-
-
Save fvisticot/c1523398f3fa0a1f5a312a39e7567b6c to your computer and use it in GitHub Desktop.
Exceptions
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() { | |
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