2013-12-12 44 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塊,只要你喜歡,針對不同的異常類型。未明確捕獲的任何異常類型仍將被拋出。