```c
void example1() {
object ob ;
mixed err ;
err = catch( ob = load_object("/obj/weapon/sword") ) ;
if(err) throw("加载指定文件时出错。") ;
}
void example2() {
mixed err = catch {
string file, *files = ({
"/u/g/gesslar/one", // 正常文件
"/u/g/gesslar/two", // 有问题的文件
"/u/g/gesslar/three", // 正常文件
}) ;
foreach(file in files) load_object(file) ;
} ;
if(err) printf("ERR: %O", err) ;
}
// ERR: "*Error in loading object '/u/g/gesslar/two'"
// 捕获到的值不一定是错误消息。throw() 会把收到的值原样交回,
// 因此用一个类可以表示一种结构清晰的失败,捕获方能逐个字段
// 检查它,而不必去解析字符串。
class failure {
string kind ;
mixed detail ;
}
private void charge(object who, int amount) {
int have = who->query_coins() ;
if(have < amount)
throw(new(class failure,
kind: "insufficient_funds",
detail: amount - have)) ;
who->add_coins(-amount) ;
}
void example3(object who, int price) {
mixed err = catch( charge(who, price) ) ;
// 必须先判断 classp():err 同样有可能是驱动程序的错误字符串,
// 这种情况下 && 会在这里短路。
if(classp(err) && err.kind == "insufficient_funds")
write("你还差 " + err.detail + " 个金币。\n") ;
else if(err)
throw(err) ; // 不该由我们处理 —— 继续往上传
}
```