日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js中的自定义异常处理函数

發布時間:2024/4/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js中的自定义异常处理函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.?Can I suppress JavaScript error messages?

2.?Can I set up my own JavaScript error handler?

3.?Can I change the active JavaScript error handler??(Demo)

Question:?Can I suppress JavaScript error messages?

Answer:?Yes – although usually it is not a good design decision to do so. (Normally, you are much better off?fixing?the errors, rather than just suppressing the error messages.)

To suppress all JavaScript error messages on your HTML page, you can put the following script code in the?<HEAD>?section of your page:

<SCRIPT language="JavaScript"> <!-- function silentErrorHandler() {return true;} window.οnerrοr=silentErrorHandler; //--> </SCRIPT>

Or you can use a similar code fragment (without the?SCRIPT?tags) in an included?.js?file, if you have one. For a working code example, see this?error handling demo!

?

Question:?Can I set up my own JavaScript error handler?

Answer:?Yes. To define your own error handler, use this JavaScript code:

function handlerFunction(description,page,line) {// put error-handling operators herereturn true; } window.οnerrοr=handlerFunction;

Your error handler function can optionally use the following parameters:

  • a textual description of the error
  • the address (URL) of the page on which the error occurred
  • the number of the line in which the error occurred

The error handler function must return?false?if you wish to invoke the browser's default error handler after your own handler finishes. If you don't want to invoke the browser's default handler, your handler function must return?true. For an additional code example, check out this?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?alert box
  • 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 }

?

轉載于:https://www.cnblogs.com/oxspirt/p/6700974.html

總結

以上是生活随笔為你收集整理的js中的自定义异常处理函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。