on
keywordIn Dart, the on
keyword is used with the try
block when we have a certain type of exception to catch.
try {
// some code here
}
on exception_type {
// some code here
}
The following code shows how to use the on
keyword in Dart:
void main() {try {// declare variable x and yint x = 5, y;y = x ~/ 0;}on IntegerDivisionByZeroException {print("Error: Zero division");}}
main()
function.try
block to catch any error.main()
, we declare two variables of int type, x = 5
and y
.(x~/0)
to y
.on
to specify the exception type that occurs.