The PolyML.Exception structure

The Exception sub-structure contains functions that assist in tracing exceptions.

structure Exception :
sig
    val exceptionLocation : exn -> location option
    val raiseWithLocation : exn * location -> 'a
    val reraise : exn -> 'a

    val traceException : (unit -> 'a) * (string list * exn -> 'a) -> 'a
	 val exception_trace : (unit -> 'a) -> 'a
end

Exception packets in Poly/ML normally contain information about the location, typically the file name and line number, where the exception was raised. The exceptionLocation function extracts this information if it is available. raiseWithLocation can be used to provide an explicit location and override the default. reraise is written in terms of exceptionLocation and raiseWithLocation. It is typically used where a function needs to catch an exception and wishes to raise it again after doing some clean-up. Using the raise instruction of ML would set the location to be the clean-up code whereas reraise will retain the original location.

exception_trace and traceException can be used to produce information about the stack when an exception is raised. They both take an argument that is a function to be executed and if an exception is raised within the function that is not caught they produce information from the stack. exception_trace prints this to standard-output whereas traceException uses the second argument, a function, and calls that with the list of function names and the exception packet. Note that if the function being traced handles any exceptions the trace will only show the stack from that point down.