Created
April 26, 2020 21:38
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
// Dart code can throw and catch exceptions. Exceptions are errors indicating that something unexpected happened. | |
void main() { | |
int x = 11; | |
int y = 0; | |
int res; | |
try { | |
res = x ~/ y; | |
} on IntegerDivisionByZeroException { | |
print('Cannot divide by zero'); | |
} | |
exception(); | |
exceptionWithFinaly(); | |
} | |
// Try catch with exception | |
exception() { | |
int x = 10; | |
int y = 0; | |
int res; | |
try { | |
res = x ~/ y; | |
} catch (e) { | |
print(e); | |
} | |
} | |
// With finaly | |
exceptionWithFinaly() { | |
int x = 9; | |
int y = 0; | |
int res; | |
try { | |
res = x ~/ y; | |
} on IntegerDivisionByZeroException { | |
print('Cannot divide by zero'); | |
} finally { | |
print('Finally block executed'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment