The Exception is to indicate if something unexpected happen while executing the program.
The Exception handling is to void the termination of a program, you must catch exception properly.
If the exception isn’t caught, its terminate the program.
The try statement is definition of exception handling code in a structured way.
main() {
dynamic output = true;
try {
print(output++);
} catch (e) {
print(e); // Exception Class 'bool' has no instance method '+'.
}
}
A try statement consists of a block statement, followed by at least one of:
on-catch
clauses, each of which specifies the type of exception object to be handled, one or two exception parameters, and a block statement.Note: The on clause can be omitted.
var expr1;
try {
expr1;
} on NullThrownError catch (e1, s1) {} on Exception catch (e1, s2) {} finally {}
expr1
is executed.expr1
throws with exception object e1
and stack trace s1
, then e1
and s1
are matched against the on-catch clauses to yield a new completion.The catch
keyword is to get a reference to the exception object. Catching an exception gives you a chance to handle it:
dynamic output = true;
try {
print(output++);
} catch (e) {
print(e); // Exception Class 'bool' has no instance method '+'.
}
The finally
keyword is to ensure that some code runs whether or not an exception is thrown.
The finally clause runs after any matching catch clauses. If no catch clause matches the exception, the exception is propagated after the finally clause runs.
The following code snippet shows how to clean up resources, even if an exception is thrown.
try {
openConnection();
processData();
} catch (e) {
logError(e);
} finally {
// Always clean up, even if an exception is thrown.
closeConnection();
}
The throw expression is to throw an exception
NullThrownError
is thrown.Error
object or subclass, and it is the first time that Error
object is thrown, the stack trace will be returned by the objects stackTrace
getter property.Error
object is thrown more than once, its stackTrace getter will return the stack trace from the first time it was thrown.throw FormatException('Expected at least 1 section');
throw 'Out of Stock!';
Throwing an exception is an expression, you can throw exceptions in =>
statements, as well as anywhere else that allows expressions:
void distanceTo(Point other) => throw UnimplementedError();
The rethrow
statement is to re-throw an exception and its associated stack trace.
Execution of a rethrow statement:
Use rethrow
keyword, to partially handle an exception while allowing it to propagate.
void misbehave() {
try {
dynamic foo = true;
print(foo++); // Runtime error
} catch (e) {
print('misbehave() partially handled ${e.runtimeType}.');
rethrow; // Allow callers to see the exception.
}
}
void main() {
try {
misbehave();
} catch (e) {
print('main() finished handling ${e.runtimeType}.');
}
}
Use the on
keyword to filter for specific exceptions by type, if matching an exception object e1
and stack trace s1
against a (potentially empty) sequence of on-catch clauses of the form.
on NullThrownError catch (e1, s1) {
} on Exception catch (e1, s2) {
}
The NoSuchMethodError
is thrown when a receiving object (which might be null) does not implement a method.
The ArgumentError
is thrown by a method that encounters an unexpected argument.
The custom exception is to define your own exceptions.
Throwing an application-specific exception is a common way to indicate that an error has occurred. You can define a custom exception by implementing the Exception
interface:
class DbException implements Exception {
final String msg;
const DbException([this.msg]);
String toString() => msg ?? 'DbException';
}
The multiple catch clause is to handle code that can throw more than one type of exception.
class DbException implements Exception {
final String msg;
const DbException([this.msg]);
String toString() => msg ?? 'DbException';
}
class DbConnection {
void OpenConnection() {}
void ProcessData() {}
void CloseConnection() {}
}
main() {
var connection = DbConnection();
try {
connection.OpenConnection();
connection.ProcessData();
} on DbException {
// A specific exception
print('DbConnection fails');
} on Exception catch (e) {
// Anything else that is an exception
print('Unknown exception. $e');
} catch (e) {
// No specified type, handles all
print('Something really unknown: $e');
}
}