• 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.
       
      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! 
      '
       +'The error was handled by '
       +'a customized error handler.
      '
       +'
      Error description: 	'+desc
       +'
      Page address:      	'+page
       +'
      Line number:       	'+line
       )
       return true
      }
  • 相关阅读:
    java修改应用程序标题栏
    电动双轮车
    Oracle执行计划相关(待补充)
    SQL中IN和EXISTS用法的区别
    union all与union all
    equals与hashcode
    文件上传的整个流程
    adwords与adsence
    struts2文件上传大小
    为什么是UUID做主键
  • 原文地址:https://www.cnblogs.com/oxspirt/p/6698876.html
Copyright © 2020-2023  润新知