I know I can catch a specific Exception type in dart with the following:
try { ... } on SpecificException catch(e) { ... }
But is there a way to catch multiple specific exception types with on line instead of using multiple catch statements?
Java allows you to catch multiple type exceptions in a single catch block. It was introduced in Java 7 and helps to optimize code. You can use vertical bar (|) to separate multiple exceptions in catch block. An old, prior to Java 7 approach to handle multiple exceptions.
The Try Block If your code throws more than one exception, you can choose if you want to: use a separate try block for each statement that could throw an exception or. use one try block for multiple statements that might throw multiple exceptions.
In Java SE 7 and later, a single catch block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.
You can only specify one type per on xxx catch(e) { line or alternatively use catch(e) to catch all (remaining - see below) exception types. The type after on is used as type for the parameter to catch(e) .
You can only specify one type per on xxx catch(e) {
line or alternatively use catch(e)
to catch all (remaining - see below) exception types. The type after on
is used as type for the parameter to catch(e)
. Having a set of types for this parameter wouldn't work out well.
try { ... } on A catch(e) { ... } on B catch(e) { ... } catch(e) { // everything else }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With