Skip to main content

Error Handling

BBj uses a trap-and-branch model for error handling: you set error traps (SETERR, ERR=) that redirect execution when errors occur, and raise errors with THROW. This pattern is used throughout all BBj code -- understanding it is essential before writing anything non-trivial.

At a Glance

FeatureSyntaxPurpose
SETERRseterr labelSet global error trap
ERR=open(1,err=handler)"file"Statement-level error trap
THROWthrow "message", 256Raise custom error
ERRerrLast error number
ERRMESerrmes(-1)Last error message
RETRYretryRe-execute the statement that errored

For Java, Python, and C# Developers

TaskJavaPythonC#BBj
Set error handlertry {try:try {seterr handler
Catch specific errorcatch (IOException e)except IOError as e:catch (IOException e)if err = 17 then ...
Catch all errorscatch (Exception e)except Exception as e:catch (Exception e)seterr handler (global)
Get error messagee.getMessage()str(e)e.Messageerrmes(-1)
Raise/throwthrow new Exception("msg")raise Exception("msg")throw new Exception("msg")throw "msg", 256
Skip on errorcatch (E e) { }except: passcatch { }err=*next
Cleanupfinally { }finally:finally { }Fall-through to cleanup label

BBj does not have try/catch blocks. All error handling uses seterr to set a handler label, err= on individual statements for inline trapping, and throw to raise custom errors. For the complete cross-language reference, see BBj for Java, Python, and C# Developers.

Complete Runnable Examples

This chapter's code snippets illustrate individual concepts. For complete, runnable programs you can open directly in the BBj IDE, see the sample files in samples/04-error-handling/:

  • seterr_basic.bbj -- Basic SETERR error trapping
  • err_clause.bbj -- ERR= clause on individual statements
  • throw_validation.bbj -- THROW from a class method
  • java_error_handling.bbj -- Handling Java exceptions in BBj

See Running Samples for setup instructions.