Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

A larger example: read two numbers and divide them

We read two numbers from the user and we divide them. What could go wrong? :)

Links:

(1) example with multiple exceptions
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;
        }
    }
}
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 September 02, 13:58