Easy csharp

Try-catch divide by zero

Problem

Safely divide 10 by 0 and print friendly message on error.

Hints
  • try/catch DivideByZeroException

Your practice code

Ready — edit the code above and click Run.

Solution

using System;

class Program {
    static void Main() {
        int a = 10, b = 0;
        try {
            Console.WriteLine(a / b);
        } catch (DivideByZeroException) {
            Console.WriteLine("Cannot divide by zero");
        }
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

Catch specific exceptions first—interviewers want structured error handling, not empty catch.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details