Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch multiple specific exception types in Dart with one catch expression

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?

like image 593
Felix Schütz Avatar asked Sep 17 '15 10:09

Felix Schütz


People also ask

How do you catch multiple exceptions in a single catch?

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.

How do you handle different types of exceptions using multiple catch statements?

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.

Can we handle multiple exceptions by using single catch block?

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.

How do you catch a specific exception in darts?

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) .


1 Answers

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 } 
like image 185
Günter Zöchbauer Avatar answered Sep 27 '22 16:09

Günter Zöchbauer