import std.stdio;
import std.string; // readln(), chomp()
import std.conv;
class EOFException : Exception
{
this(string msg = "")
{
super(msg);
}
}
string input(const string msg = "")
{
write(msg);
auto line = readln();
if (line is null) // if Ctrl+D was pressed
{
throw new EOFException();
}
return line.chomp();
}
class DivisionByZeroException : Exception
{
this(string msg = "")
{
super(msg);
}
}
void main()
{
while (true)
{
try
{
auto num1 = input("1st number: ").to!float;
auto num2 = input("2nd number: ").to!float;
if (num2 == 0)
{
throw new DivisionByZeroException();
}
auto result = num1 / num2;
writefln("Result of the division: %.2f", result);
writeln("---");
}
catch (std.conv.ConvException)
{
writeln("Error: provide a number");
}
catch (DivisionByZeroException e) // if needed, you can assign the exception object to a variable
{
writeln("Error: you cannot divide by 0");
writeln(e); // print the exception object
}
catch (EOFException)
{
writeln();
writeln("bye.");
break;
}
}
}