Recap - Safe Division
Part of the Logic & Flow section of Coddy's Dart journey — lesson 43 of 65.
Challenge
EasyCreate a program that implements a safe division calculator using exception handling. Your program should handle division by zero errors gracefully and provide clear feedback to users when errors occur.
- Read a string input representing the first number (dividend)
- Read a string input representing the second number (divisor)
- Create a function called
safeDivisionthat takes two double parameters and returns a double result - The function should use a
try-catchblock to handle potential division errors: - In the
tryblock: Check if the divisor is zero and throw an exception with the message"Division by zero is not allowed"if true, otherwise perform the division - In the
catchblock: Handle the exception and return0.0as a safe default value - Convert the input strings to double values using
double.parse() - Call the
safeDivisionfunction with the converted values - Display the results in the exact format shown below
For example, if the inputs are "10.0" and "2.0", your program should output:
Safe Division Calculator
========================
First number: 10.0
Second number: 2.0
Performing division...
Result: 5.0
Operation completed successfullyIf the inputs are "15.0" and "0.0", your program should output:
Safe Division Calculator
========================
First number: 15.0
Second number: 0.0
Performing division...
Error: Division by zero is not allowed
Using safe default value: 0.0
Operation completed with error handlingIf the inputs are "7.5" and "2.5", your program should output:
Safe Division Calculator
========================
First number: 7.5
Second number: 2.5
Performing division...
Result: 3.0
Operation completed successfullyYour program must implement the safeDivision function that uses a try-catch block to handle division by zero. In the try block, check if the divisor equals 0.0 and use the throw keyword to manually trigger an exception if true. In the catch block, print the error message and return 0.0 as a safe fallback value. The main program should call this function and display different completion messages based on whether an error occurred.
Try it yourself
import 'dart:io';
// TODO: Create the safeDivision function here
void main() {
// Read input
String? firstInput = stdin.readLineSync();
String? secondInput = stdin.readLineSync();
// Convert strings to double
double firstNumber = double.parse(firstInput!);
double secondNumber = double.parse(secondInput!);
// Display header
print("Safe Division Calculator");
print("========================");
print("First number: $firstNumber");
print("Second number: $secondNumber");
print("Performing division...");
// TODO: Call the safeDivision function and handle the result
// Remember to check if an error occurred to display the appropriate completion message
}All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List6Basic Error Handling
What are Exceptions?The 'try-catch' BlockCatching Exceptions with 'on'The 'finally' BlockThrowing an ExceptionRecap - Safe Division