How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the D programming language

Published on 12 May 2024 09:40 PM
#D

How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the D programming language

Table of Contents

Problem Statement

Show how to create a user-defined exception   and   show how to catch an exception raised from several nested calls away.

Show/describe what happens when the program is run.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Exceptions/Catch an exception thrown in a nested call step by step in the D programming language

Source code in the d programming language

class U0 : Exception {
    this() @safe pure nothrow { super("U0 error message"); }
}

class U1 : Exception {
    this() @safe pure nothrow { super("U1 error message"); }
}

void foo() {
    import std.stdio;

    foreach (immutable i; 0 .. 2) {
        try {
            i.bar;
        } catch (U0) {
            "Function foo caught exception U0".writeln;
        }
    }
}

void bar(in int i) @safe pure {
    i.baz;
}

void baz(in int i) @safe pure {
    throw i ? new U1 : new U0;
}

void main() {
    foo;
}


  

You may also check:How to resolve the algorithm Plot coordinate pairs step by step in the Go programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Racket programming language
You may also check:How to resolve the algorithm Equilibrium index step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Voronoi diagram step by step in the Prolog programming language
You may also check:How to resolve the algorithm Input loop step by step in the Phix programming language