javascript 自定义错误处理

php 中是可以自定义程序的错误和异常处理函数的(handler).于是,我在想,javascript 中是否也存在和PHP中一样的异常和错误处理函数呢?

try{}catch(){} 这种捕捉异常和错误的机制,我们很熟悉,当然这在javascript 中也是支持的。那么,javascript 中有类似php中的魔术方法吗?来定义

异常和错误的handler()????

于是,google之,找到了下面的这篇文章,确实可以:)!!!!!!

参考: http://www.javascripter.net/faq/err_demo.htm?customHandler

Error Handling Demo

 Contents | JavaScript FAQ | Error Handling  

Question: Can I dynamically change the JavaScript error handler?

Answer: Yes. To change the JavaScript error handler, just setwindow.onerror to the name of the function that will serve as your new error handler.

Here's a demo that lets you test three different error handlers:

  • the browser's default error handler
  • an error handler that displays a customized alert box
  • a "silent" error handler that suppresses all error messages.  Custom Error Handler Silent Error Handler Default Error Handler  
    1. Use the select box to set or change the error handler.
    2. Click Fire an Error to test the active error handler.

    Below is the source code of the error handling functions used in this demo:

    function defaultHandler() {return false}
    function silentHandler()  {return true}
    function customHandler(desc,page,line,chr)  {
     alert(
      'JavaScript error occurred! \n'
     +'The error was handled by '
     +'a customized error handler.\n'
     +'\nError description: \t'+desc
     +'\nPage address:      \t'+page
     +'\nLine number:       \t'+line
     )
     return true
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值