-
Notifications
You must be signed in to change notification settings - Fork 4
Imperative error handling
tim-hardcastle edited this page Oct 15, 2023
·
12 revisions
Charm commands don't return values. But everything can return an error! The way Imperative Charm works with this is demonstrated in examples/try.ch.
The try keyword acts as a conditional that succeeds if an error is not returned:
tryWithoutCapture :
try :
get x from MadeUpThing()
else :
post "Well, that didn't work." to Output()
If try is followed by the name of a variable, and an error is returned, then an error will be assigned to that variable.
tryWithCapture :
try e :
get x from MadeUpThing()
else :
post "Well, that didn't work." to Output()
post e[errorMessage] to Output()
Note that the error can only be assigned to a local variable: it is never possible to assign an error value to a global variable.
try works in other ways like an ordinary conditional. For example, it doesn't have to be the first condition in its block:
dividePotentialNulls(a, b int?) :
a in null or b in null :
post NULL to Output()
try :
post a / b to Output()
else :
error "tried to divide by zero"
🧿 Pipefish is distributed under the MIT license. Please steal my code and ideas.
- Getting started
- Language basics
- The type system and built-in functions
- Functional Pipefish
- Encapsulation
- Imperative Pipefish
-
Imports and libraries
- The crypto/aes library
- The crypto/bcrypt library
- The crypto/rand library
- The crypto/rsa library
- The crypto/sha_256 library
- The crypto/sha_512 library
- The database/sql library
- The encoding/base_32 library
- The encoding/base_64 library
- The encoding/csv library
- The encoding/json library
- The files library
- The fmt library
- The html library
- The image library
- The image/bmp library
- The image/color library
- The image/jpeg library
- The image/png library
- The lists library
- The markdown library
- The math library
- The math/big library
- The math/cmplx library
- The math/rand library
- The net/http library
- The net/mail library
- The net/smtp library
- The net/url library
- The os/exec library
- The path library
- The path/filepath library
- The reflect library
- The regexp library
- The strconv library
- The strings library
- The terminal library
- The time library
- The unicode library
- Advanced Pipefish
- Developing in Pipefish
- Deployment
- Appendices