dart
undantag
Sök…
Anmärkningar
Dartkod kan kasta och fånga undantag. Undantag är fel som indikerar att något oväntat hände. Om undantaget inte fångas avbryts isolatet som höjde undantaget och typiskt avslutas isolatet och dess program.
Till skillnad från Java är alla Darts undantag okontrollerade undantag. Metoder förklarar inte vilka undantag de kan kasta, och du är inte skyldig att fånga några undantag.
Dart ger Undantag och Fel typer, samt ett flertal fördefinierade subtyper. Du kan naturligtvis definiera dina egna undantag. Dart-program kan emellertid kasta alla objekt som inte är null, inte bara Exception och Error-objekt, som ett undantag.
Anpassat undantag
class CustomException implements Exception {
String cause;
CustomException(this.cause);
}
void main() {
try {
throwException();
} on CustomException {
print("custom exception is been obtained");
}
}
throwException() {
throw new CustomException('This is my first custom exception');
}
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow