Error Handling
Throwing Exceptions
Use throw to raise an exception:
float Divide(float a, float b){ if (b == 0.0) { throw Exception("Division by zero"); } return a / b;}try / catch / finally
Catch exceptions with a try block:
try{ float result = Divide(10.0, 0.0); Console.WriteLine($"{result}");}catch (Exception e){ Console.WriteLine("Error: " + e.Message);}finally{ Console.WriteLine("Done.");}The finally block always runs, whether or not an exception was thrown.
Bare Catch
A bare catch; swallows any exception silently:
try{ RiskyOperation();}catch;Or with a recovery block:
try{ RiskyOperation();}catch{ Console.WriteLine("Something went wrong");}Catching Specific Types
Catch blocks match exception types from most specific to least specific:
type NetworkError { int code; }type TimeoutError { int elapsed; }
try{ FetchData();}catch (NetworkError e){ Console.WriteLine("Network error");}catch (TimeoutError e){ Console.WriteLine("Timed out");}catch;Re-throwing
Re-throw the current exception with a bare throw;:
try{ RiskyOperation();}catch (Exception e){ LogError(e); throw;}Exception Properties
Message
The Message property returns the exception’s error message:
catch (Exception e){ Console.WriteLine(e.Message);}StackTrace
The StackTrace property returns the call stack as an iterable collection of StackFrame objects:
catch (Exception e){ for (StackFrame frame : e.StackTrace) { Console.WriteLine((string)frame); }}Each StackFrame has three fields:
| Field | Type | Description |
|---|---|---|
FunctionName | string | The function name |
FileName | string | The source file name |
LineNumber | int | The line number |
StackFrame has a string conversion operator that returns a formatted string like "at DoWork() in main.ce:42".
Built-in Exception Types
| Type | Description |
|---|---|
Exception | Base type — all exceptions inherit Message and StackTrace |
NullReferenceException | Null pointer dereference |
StackOverflowException | Stack overflow from infinite recursion |
DivideByZeroException | Integer division or modulo by zero |
OverflowException | Arithmetic overflow in checked blocks |
IndexOutOfRangeException | Array index out of bounds |
FFIException | Unresolved FFI symbol called at runtime |
OutOfMemoryException | GC heap memory limit exceeded |
ExecutionTimeoutException | Sandbox execution timeout expired |
Runtime trap exceptions (NullReferenceException, DivideByZeroException, OverflowException, IndexOutOfRangeException) are raised automatically by the compiler’s safety checks — no explicit throw is needed. They are catchable via try/catch like any user-thrown exception.
Checked / Unchecked Arithmetic
The checked and unchecked keywords control overflow detection for integer arithmetic.
// Checked: overflow throws OverflowExceptionchecked{ int x = 2147483647 + 1; // throws OverflowException}
// Unchecked: overflow wraps silently (default behavior)unchecked{ int x = 2147483647 + 1; // wraps to negative, no exception}Both support single-statement form:
checked int y = a * b;unchecked int z = a * b;Nesting is supported — the inner context overrides the outer:
checked{ int safe = a + b; // overflow-checked unchecked { int fast = c + d; // wraps silently }}Inside checked blocks, integer +, -, and * use overflow-checked instructions. If the operation overflows, an OverflowException is thrown.
Division by Zero Protection
Integer division (/) and modulo (%) always check for a zero divisor, regardless of checked/unchecked context:
int x = 10 / 0; // throws DivideByZeroExceptionint y = 10 % 0; // throws DivideByZeroExceptionSee Also
- FFI — Foreign Function Interface for calling native code
- Assertions —
assertandconst assertfor invariant checking