Get your
own
website
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Change Language
Go to Spaces
Ctrl+Alt+P
class OrderException implements Exception { final String message; OrderException(this.message); @override String toString() => '$runtimeType: $message'; } class OutOfStockException extends OrderException { OutOfStockException(String product) : super('$product is out of stock'); } class InvalidQuantityException extends OrderException { InvalidQuantityException(int quantity) : super('$quantity is not a valid quantity'); } void order(String product, int quantity) { if (quantity <= 0) { throw InvalidQuantityException(quantity); } if (product == 'Mango') { throw OutOfStockException(product); } print('Ordered $quantity $product'); } void main() { var orders = [('Apple', 3), ('Mango', 2), ('Banana', 0)]; for (var (product, quantity) in orders) { try { order(product, quantity); } on OutOfStockException catch (e) { print('Sorry! $e'); } on OrderException catch (e) { print('Order failed. $e'); } } }
Ordered 3 Apple
Sorry! OutOfStockException: Mango is out of stock
Order failed. InvalidQuantityException: 0 is not a valid quantity