Menu
Coddy logo textTech

Recap - Safe Division

Part of the Logic & Flow section of Coddy's Dart journey — lesson 43 of 65.

challenge icon

Challenge

Easy

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

  1. Read a string input representing the first number (dividend)
  2. Read a string input representing the second number (divisor)
  3. Create a function called safeDivision that takes two double parameters and returns a double result
  4. The function should use a try-catch block to handle potential division errors:
    • In the try block: 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 catch block: Handle the exception and return 0.0 as a safe default value
  5. Convert the input strings to double values using double.parse()
  6. Call the safeDivision function with the converted values
  7. 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 successfully

If 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 handling

If 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 successfully

Your 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