1. 程式人生 > >[譯]Javascript中的錯誤信息處理(Error handling)

[譯]Javascript中的錯誤信息處理(Error handling)

java https ror 信息處理 esc execute 函數 丟失 youtube

本文翻譯youtube上的up主kudvenkat的javascript tutorial播放單

源地址在此:

https://www.youtube.com/watch?v=PMsVM7rjupU&list=PL6n9fhu94yhUA99nOsJkKXBqokT3MBK0b

在Javascript中使用try/catch/finally來處理runtime的錯誤.這些runtime錯誤被稱為exceptions.各種各樣的原因都可能導致exception.比如,使用沒有申明的變量或者方法都可能導致exception.

可能導致exceptions的Javascript語句都應該被包裹在try語句內.當try語句內的某一句導致exception的時候,控制權就立刻轉移到catch語句內,然後跳過try語句內的剩下所有代碼內容.

Javascript try catch例子:

try 
{
    // Referencing a function that does not exist cause an exception
    // 使用不存在的函數導致產生一個exception
    document.write(sayHello());
    // Since the above line causes an exception, the following line will not be executed
    // 因為以上的代碼導致出現exception,所以這以下的代碼將不會運行
   document.write("This line will not be executed"); }
// When an exception occurs, the control is transferred to the catch block //當exception產生後,控制權就轉交到了catch語句塊中

catch (e) { document.write("Description = " + e.description + "[br/]"); document.write("Message = " + e.message + "[br/]"); document.write("Stack = " + e.stack + "[br/][br/]"); } document.write(
"This line will be executed");

Output : // JavaScript try catch example.png

請註意:一個try語句塊應該始終緊跟一個catch語句塊或者一個finally語句塊,或者兩者兼有

finally語句塊無論exception是否產生都會運行.其主要用處在於清理和釋放腳本運行期間所占用的資源.舉個例子,如果你的try語句塊打開了一個文件且運行,而好死不死又發生了一個exception,那麽finally語句塊就是用來關閉文件的.

Javascript try catch finally例子:

try 
{
    // Referencing a function that does not exist cause an exception
    document.write(sayHello());
    // Since the above line causes an exception, the following line will not be executed
    document.write("This line will not be executed");
}
// When an exception occurs, the control is transferred to the catch block
catch (e) 
{
    document.write("Description = " + e.description + "[br/]");
    document.write("Message = " + e.message + "[br/]");
    document.write("Stack = " + e.stack + "[br/][br/]");
}
finally 
{
    document.write("This line is guaranteed to execute");
}

Output : JavaScript try catch finally example.png

Javascript中的格式錯誤與exceptions需要註意的是:

try/catch/finally語句塊可以捕捉到exceptions,但是格式錯誤則無法被其捕捉到.

例子:以下代碼中有一個格式錯誤-丟失了閉小括號.catch語句塊則無法捕捉到這個錯誤

try 
{
    alert("Hello";
}
catch (e) 
{
    document.write("JavaScript syntax errors cannot be caught in the catch block");       
}

Javascript throw語句:用throw語句來捕捉自定義exceptions

Javascript throw exception例子:

JavaScript throw exception example :
function divide() 
{
    var numerator = Number(prompt("Enter numerator"));
    var denominator = Number(prompt("Enter denominator"));

    try 
    {
        if (denominator == 0) 
        {
            throw {
                error: "Divide by zero error",
                message: "Denominator cannot be zero"
            };
        }
        else 
        {
            alert("Result = " + (numerator / denominator));
        }

    }
    catch (e) 
    {
        document.write(e.error + "[br/]");
        document.write(e.message + "[br/]");
    }
}

divide();

[譯]Javascript中的錯誤信息處理(Error handling)