How to resolve the algorithm Exceptions step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Exceptions step by step in the Wren programming language

Table of Contents

Problem Statement

This task is to give an example of an exception handling routine and to "throw" a new exception.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Exceptions step by step in the Wren programming language

Source code in the wren programming language

var intDiv = Fn.new { |a, b|
    if (!(a is Num && a.isInteger) || !(b is Num && b.isInteger)) Fiber.abort("Invalid argument(s).")
    if (b == 0) Fiber.abort("Division by zero error.")
    if (a == 0) a = a.badMethod()
    return (a/b).truncate
}

var a = [ [6, 2], [6, 0], [10, 5], [true, false], [0, 2] ]
for (e in a) {
    var d
    var f = Fiber.new { d = intDiv.call(e[0], e[1]) }
    f.try()
    if (f.error) {
        System.print("Caught %(f.error)")
    } else {
        System.print("%(e[0]) / %(e[1]) = %(d)")
    }
}

  

You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the Erlang programming language
You may also check:How to resolve the algorithm XML/Input step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Summarize and say sequence step by step in the TXR programming language
You may also check:How to resolve the algorithm Draw a clock step by step in the Rust programming language
You may also check:How to resolve the algorithm Program name step by step in the J programming language