Created
December 17, 2019 01:10
-
-
Save aabeben/f898ab9a905b02266af7087dbb21f2fd 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
// Exceptions | |
// Untuk membangkitkan sebuah eksepsi, gunakan throw: | |
if(astonauts == 0){ | |
throw StateError('No astronauts.'); | |
} | |
// Untuk menangkap sebuah eksepsi, gunakan sebuah pernyataan try dengan on atau | |
// catch (atau keduanya) | |
try{ | |
for(var object in flybyObjects){ | |
var description = await File('$object.txt').readAsString(); | |
print(description); | |
} | |
} on IOException catch(e){ | |
print('Could not describe object: $e'); | |
} finally{ | |
flybyObjects.clear(); | |
} | |
// Catatan bahwa kode di atas adalah asynchronous; | |
// try bekerja baik pada kode synchronous dan kode di dalam fungsi async |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment