2013-12-12 46 views
5

如何用cfscript中的try-catch捕获自定义异常?用cfscript捕捉自定义异常

<cffunction name="myFunction"> 
    <cfset foo = 1> 

    <cfif foo EQ 1> 
    <cfthrow type="customExcp" message="FAIL!"> 
    </cfif> 
</cfif> 

try-catch是在cfscript中。什么应该进入catch()声明?

try { 
    myFunction(); 
} catch() { 
    writeOutput("Ooops"); 
} 
+0

我不知道,但我会尝试的第一件事是“扔”。 –

回答

9

詹姆斯指出你在他的回答中的文档,但他错过了你问定制例外位。语法是:

try { 
    myFunction(); 
} catch (customExcp e) { 
    writeOutput("Ooops"); 
    writeDump(e); // have a look at the contents of this 
} 

注意,你可以有很多catch块,只要你喜欢,针对不同的异常类型。未明确捕获的任何异常类型仍将被抛出。