Skip to content

Commit eb7febb

Browse files
Add Examples / Unit Tests for Exceptions (#775)
## Motivation Resolves #480 This module demonstrates exception handling combinators in Effekt using a simple, focused example. By showcasing custom exception handling (`TestException`) with different strategies (default handling, ignoring, reporting, finalizing), it provides a practical guide for understanding these features. ## Changes - Introduced `TestException` as a custom exception type, independent of other exceptions like `OutOfBounds`. - Added `generalOperation`, which raises a `TestException` for invalid input or returns a string. - Included tests for handling exceptions with: - Default behavior - Ignoring exceptions - Reporting exceptions - Finalization hooks - "Reifying" with Results ## Testing The examples are self-contained in the `main` function, covering all provided exception-handling strategies. Each approach is tested and outputs expected results for validation (`combinators.check`).
1 parent df1adc5 commit eb7febb

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Test: Default Handling
2+
hello
3+
hello
4+
Error: Invalid index (0)
5+
Error: Invalid index (-1)
6+
hello
7+
Test: Finalizer
8+
hello
9+
Test: Finalizer
10+
Error: Invalid index (0)
11+
Success: hello
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
module examples/pos/exception/combinators
2+
3+
import exception
4+
import result
5+
6+
7+
/// Used as a type for `Exception` purely for independent testing
8+
record TestException()
9+
10+
11+
/// Returns the string if index > 0; otherwise raises a TestException.
12+
def generalOperation(str: String, index: Int): String / Exception[TestException] = {
13+
if (index <= 0)
14+
do raise(TestException(), "Error: Invalid index (" ++ show(index) ++ ")")
15+
else
16+
str
17+
}
18+
19+
def main() = {
20+
val str: String = "hello"
21+
22+
// Test for default handling of TestException
23+
def defaultTestException { p: => String / Exception[TestException] }: Unit = {
24+
with on[TestException].default { println("Test: Default Handling") }
25+
println(p().show)
26+
}
27+
28+
defaultTestException { str.generalOperation(0) } // Test: Default Handling
29+
defaultTestException { str.generalOperation(1) } // hello
30+
31+
// Test for ignoring TestException
32+
def ignoreTestException { p: => String / Exception[TestException] }: Unit = {
33+
with on[TestException].ignore();
34+
println(p().show)
35+
}
36+
37+
ignoreTestException { str.generalOperation(0) } // *ignores the TestException*
38+
ignoreTestException { str.generalOperation(1) } // hello
39+
40+
// Test for reporting TestException
41+
def reportTestException { p: => String / Exception[TestException] }: Unit = {
42+
with on[TestException].report();
43+
println(p().show)
44+
}
45+
46+
reportTestException { str.generalOperation(0) } // Error: Invalid index (0)
47+
reportTestException { str.generalOperation(-1) }// Error: Invalid index (-1)
48+
reportTestException { str.generalOperation(1) } // hello
49+
50+
// Test for finalizing TestException
51+
def finalizeTestException { p: => String / Exception[TestException] }: Unit = {
52+
try {
53+
with on[TestException].finalize { println("Test: Finalizer") }
54+
println(p().show)
55+
} with Exception[TestException] { def raise(exception, msg) = () }
56+
}
57+
58+
finalizeTestException { str.generalOperation(0) } // Test: Finalizer
59+
finalizeTestException { str.generalOperation(1) } // Test: Finalizer hello
60+
61+
// Test for "reifying" an Exception using Result
62+
def resultTestException { p: => String / Exception[TestException] }: Unit = {
63+
val res = result[String, TestException] { p() }
64+
res match {
65+
case Success(msg) => println("Success: " ++ msg)
66+
case Error(exc, msg) => println(msg)
67+
}
68+
}
69+
70+
resultTestException { str.generalOperation(0) } // Error: Invalid index (0)
71+
resultTestException { str.generalOperation(1) } // Success: hello
72+
73+
}

0 commit comments

Comments
 (0)