如何利用simple-error子类提供的格式控制

时间:2012-06-22 10:04:35

标签: error-handling common-lisp

我目前正在使用cxml-stp框架进行开发,但在解析时,我确实得到cxml-stp:stp-error,这是simple-error的子类,这被记录为一种错误,其中提供格式控制。

如何打印错误信息?由于两个API都没有提供任何特定的功能,简单的FORMAT只会导致对象被打印但不使用提供的FORMAT字符串。

e.g。

(SB-KERNEL:CASE-FAILURE
 ETYPECASE
 #<CXML-STP:STP-ERROR "text includes characters that cannot be ~
                represented in XML at all: ~S"
   {1007814951}>
 (STRING SIMPLE-STRING))

2 个答案:

答案 0 :(得分:3)

只需编写条件对象而不转义:

(write condition :escape nil)

答案 1 :(得分:1)

(defun try-handle-error (err)
  (handler-case
      (error err)
    (serious-condition (condition)
      (apply #'format
         (nconc (list t)
            (cons (simple-condition-format-control condition)
              (simple-condition-format-arguments condition)))))))

(try-handle-error (make-condition
        'simple-error
        :format-control "say something ~s~&"
        :format-arguments '(42)))

这就是一个例子。基本上,格式控制和格式参数是在简单错误类上声明的槽读取器。处理错误时,可以在该错误上调用它们以获取在创建期间收到的值。