Skip to content

Error Handling

Try / catch — prøv / fang

Wrap code that might fail in a prøv block. If an error is raised (either thrown manually or triggered by the runtime), the fang block receives it as a string:

brunost
prøv {
  låst result er 10 / 0
} fang (err) {
  terminal.skriv("Caught: " + err)   // Caught: DivisionByZero
}

Both fang and endelig are optional, but you must have at least one of them:

brunost
// try + catch only
prøv {
  riskyOperation()
} fang (err) {
  terminal.skriv("Error: " + err)
}

// try + finally only
prøv {
  openResource()
} endelig {
  closeResource()
}

// all three
prøv {
  doWork()
} fang (err) {
  terminal.skriv("Failed: " + err)
} endelig {
  cleanup()
}

Finally — endelig

The endelig block always executes, regardless of whether an error occurred:

brunost
bruk terminal

prøv {
  terminal.skriv("Working...")
  kast "something went wrong"
} fang (err) {
  terminal.skriv("Caught: " + err)
} endelig {
  terminal.skriv("Always runs")
}
// Output:
// Working...
// Caught: something went wrong
// Always runs

Throwing — kast

kast raises any value as an error:

brunost
gjer divide(a, b) {
  viss (b erSameSom 0) gjer {
    kast "Cannot divide by zero"
  }
  gjevTilbake a / b
}

prøv {
  terminal.skriv(divide(10, 0))
} fang (err) {
  terminal.skriv("Error: " + err)
}

Thrown values are converted to strings when caught.

Runtime errors

The following errors are raised automatically by the runtime. All can be caught with fang:

ErrorCause
TypeErrorOperation on incompatible types
UndefinedVariableReading a variable that hasn't been declared
ImmutableAssignmentAssigning to a låst variable
DivisionByZeroDividing an integer by zero
IndexOutOfBoundsliste.hent with an out-of-range index
KeyNotFoundkart.hent with a missing key
UnknownModulebruk of a module that doesn't exist
UndefinedFieldAccessing a struct field that doesn't exist
ImmutableFieldAssigning to a låst struct field
NotAStructTypeUsing a non-type value as a constructor
OutOfMemoryMemory allocation failure

Native-only errors

ErrorCause
FileNotFoundFile path doesn't exist
PermissionDeniedOS denied file/network access
ConnectionRefusedTCP connection rejected
AddressInUsePort already bound
TimeoutNetwork operation timed out

Nested try blocks

prøv blocks can be nested:

brunost
prøv {
  prøv {
    kast "inner error"
  } fang (inner) {
    terminal.skriv("Inner caught: " + inner)
    kast "rethrown"
  }
} fang (outer) {
  terminal.skriv("Outer caught: " + outer)
}