Created
March 2, 2025 10:26
-
-
Save nicol-ograve/fd79b079b7f7ff50081b193a5e072677 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Funzione che simula una divisione, ma lancia un'eccezione se il divisore è zero | |
double dividi(double a, double b) { | |
if (b == 0) { | |
throw ArgumentError("Errore: impossibile dividere per zero!"); | |
} | |
return a / b; | |
} | |
// Funzione che gestisce l'errore ma lo rilancia se è un problema grave | |
void calcola() { | |
try { | |
print("Risultato: ${dividi(10, 0)}"); // Provoca un errore | |
} catch (e) { | |
print("Eccezione catturata: $e"); | |
rethrow; // Propaga l'eccezione dopo averla gestita | |
} finally { | |
print("Operazione terminata (che sia riuscita o meno)."); | |
} | |
} | |
void main() { | |
try { | |
calcola(); | |
} catch (e) { | |
print("Errore propagato gestito nel main: $e"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment