50 likes | 113 Views
CS 480/680 – Comparative Languages. Handling Exceptions in Scheme. exn : not instantiated directly user : raised by calling error variable : unbound global variable at run-time keyword : attempt to change the binding of a global keyword application : not instantiated directly
E N D
CS 480/680 – Comparative Languages Handling Exceptionsin Scheme
exn : not instantiated directly user : raised by calling error variable : unbound global variable at run-time keyword : attempt to change the binding of a global keyword application : not instantiated directly arity : application with the wrong number of arguments type : wrong argument type to a procedure, not including divide-by-zero mismatch : bad argument combination (e.g., out-of-range index for a vector) or platform-specific integer range error divide-by-zero : divide by zero continuation : attempt to cross a continuation boundary or apply another thread's continuation else : fall-through in cond or case struct : the supertype expression in a struct form returned a value that was not a structure type value object : object-, class-, or interface-specific error unit : unit- or unit/sig-specific error syntax : syntax error, but not a read error read : read parsing error eof : unexpected end-of-file i/o : not instantiated directly port : not instantiated directly read : error reading from a port write : error writing to a port closed : attempt to operate on a closed port user : user-defined input port returned a non-character from the character-getting procedure filesystem : illegal pathname or error manipulating a filesystem object tcp : TCP errors thread : raised by call-with-custodian misc : low-level or MzScheme-specific error unsupported : unsupported feature user-break : asynchronous thread break out-of-memory : out of memory
Catching Exceptions • (with-handlers ( (test? handler) (test? handler) … ) (code)) Macros
Catching Exceptions: Example (define div-w-inf (lambda (n d) (with-handlers ([exn:application:math:zero? (lambda (exn) +inf.0) ] ) (/ n d) ) ) ) Macros
Throwing Excpetions • You can raise your own exception using: (error “error message”) • This raises an exception of type exn:user with the error message you specify • If uncaught, this well exit program execution Macros