php 中是可以自定义程序的错误和异常处理函数的(handler).于是,我在想,javascript 中是否也存在和PHP中一样的异常和错误处理函数呢?
try{}catch(){} 这种捕捉异常和错误的机制,我们很熟悉,当然这在javascript 中也是支持的。那么,javascript 中有类似php中的魔术方法吗?来定义
异常和错误的handler()????
于是,google之,找到了下面的这篇文章,确实可以:)!!!!!!
参考:
Error Handling Demo | | | | | 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
- a "silent" error handler that suppresses all error messages. Custom Error Handler Silent Error Handler Default Error Handler
- Use the select box to set or change the error handler.
- 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} |