简体   繁体   English

在JavaScript中实现异常处理

[英]Implementing exception handling in JavaScript

I would like to know the best and easiest way to implement exception handling in Javascript. 我想知道在Java语言中实现异常处理的最佳和最简便的方法。 I'm looking for a "one point" solution. 我正在寻找“单点”解决方案。 I've got lot of functions and wouldn't want to go around and implement try-catch. 我有很多功能,不想四处走动并实现try-catch。

I feel window.onerror would be a better approach, since I just have to implement this in one place and any exceptions that occurs in any of the functions, I would be able to handle it using window.onerror. 我觉得window.onerror会是一种更好的方法,因为我只需要在一个地方实现它,并且任何函数中都会发生任何异常,我就可以使用window.onerror来处理它。 However, this seems to be supported only in IE. 但是,这似乎仅在IE中受支持。 Is there any similar way that I can use so that it supports all major standard browsers. 有没有类似的方法可以使用,以支持所有主要的标准浏览器。

Thanks in advance. 提前致谢。

Using try..catch block 使用try..catch块

try..catch block in JavaScript is very much similar to the regular C# try..catch block. JavaScript中的try..catch块与常规C#try..catch块非常相似。 The suspected code will be put in try block and all exceptions which will occur in the try block will be caught in catch block. 可疑代码将被放入try块,并且try块中将发生的所有异常都将被捕获在catch块中。

window.onload = function()
{
    try
    {
        var x = 90;
        var value = x / y;
    }
    catch(err)
    {
        document.write(err.name + ": " + err.message + "<br/>");
    }
}

Output: 输出:

TypeError: 'y' is undefined TypeError:“ y”未定义

In catch you will get the object containing type and description of the exception. 在catch中,您将获得包含异常类型和描述的对象。 More over you can also use finally block in the same way as you use in C#. 此外,您还可以像在C#中一样使用finally块。

window.onload = function()
{
    try
    {
        var x = 90;
        var value = x / y;
    }
    catch(err)
    {
        document.write(err.name + ": " + err.message + "<br/>");
    }
    finally
    {
        alert('This is finally block');
    }
}

Using onerror event 使用onerror事件

onerror event will be raised each time there is any error while performing a action in the document. 每当在文档中执行操作时发生任何错误,都会引发onerror事件。 This like on place exception handling similar to Application_Error in ASP.NET. 这类似于就地异常处理,类似于ASP.NET中的Application_Error。 Here is sample code which demonstrate this: 以下是示例代码,演示了这一点:

window.onload = function()
{
    var x = 90;
    var value = x / y;
}

window.onerror = function(errorMeaage, fileName, lineNumber)
{
    document.write('Error: ' + errorMeaage);
}

Using jQuery Solution 使用jQuery解决方案

It is similar to using onerror but with jQuery syntax. 它类似于使用onerror,但使用jQuery语法。 The syntax is: 语法为:

$(window).error(
    function(errorMeaage, fileName, lineNumber)
    {
        // handle error here
    }
);

Excerpt from JavaScript Exception Handling Techniques JavaScript Exception Handling Techniques摘录

If there are just a handful of functions calling each other, I'd do something like this. 如果只有少数几个函数互相调用,我会做这样的事情。

try{
 main();
}
catch(e){
 //error
}

Where main() would be the function that calls everything else; 其中main()是调用其他所有函数的函数; a la, the entry point within your script. la,即脚本内的入口点。 Although, this doesn't leave you with a lot of flexibility in the code called below. 虽然,这在下面的代码中没有给您太多灵活性。 So consider making different "levels" of functions, where each call to them is wrapped in a try...catch block where you can handle them appropriately. 因此,请考虑制作不同的功能“级别”,其中对它们的每次调用都包装在try ... catch块中,您可以在其中适当地处理它们。 ie. 即。

function a(){}
function b(){}

function main(){

 //call a, catch any errors
 try{
  a();
 }
 catch(e){}

 //call b, catch any errors
 try{
  b();
 }
 catch(e){}

 //and so on...

}

main();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM