Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:46:46+00:00 2026-06-08T21:46:46+00:00

For error reporting, I would like to insert a try-catch wrapper around the code

  • 0

For error reporting, I would like to insert a try-catch wrapper around the code of every function I have.

So basically I want to replace

function foo(arg){
   bar();
}

…with…

function foo(arg){
    try {
        bar() 
    }
    catch(e){
        customErrorHandler(e)
    }
}

Is there a way to apply this generic try-catch thing to all functions without manually editing all of them? For example by modifying the prototype of the Function object?

EDIT

Why I want to try-catch all my functions:
I am building an HTML5 app that I’m publishing on iOS and Android. I can tell from my current rudimentary javascript error reporting that even though the app runs nicely on my own device, errors do occur on some other devices.

My objective is twofold: whenever a javascript error occurs on someone’s device…

  1. I want to notify the user that the app may not function perfectly
  2. I want to know roughly where the error occurred, so I know where to look for the problem
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:46:48+00:00Added an answer on June 8, 2026 at 9:46 pm

    This isn’t simple since there is no way to find all JavaScript function defined everywhere. For example, any such approach would probably miss callback functions which are defined at runtime.

    You also probably don’t want to wrap all functions because that would include browser functions and functions from JavaScript libraries that you certainly don’t want to wrap.

    A much better approach is probably to define a function which wraps another function:

    var tcWrapper = function(f) {
        return function() {
            try {
                f.apply(this, arguments);
            } catch(e) {
                customErrorHandler(e)
            }
        }
    }
    

    Now you can use this function to decorate anything that you want. Wrapping will become more simple if you use name spaces:

    var NS = { f: function() {  } }
    

    Just put all functions to wrap in a special namespace and then iterate over the namespace:

    $.each( NS, function(i,n) {
        var p = NS[i];
        if( typeof p === 'function' ) {
            NS[i] = tcWrapper(p);
        }
    } );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement Error Reporting and Recovery in JavaCC grammar I have
I have this consumer targeted application that has built in error reporting. Sometimes I
I have an image with 595x842px dimensions, I would like to use the image
I have a SQL Server Reporting Services (2008) chart (error chart). The X-axis has
I'm modifying Python code that have this form: def foo(self): try: connect socket except
I just enabled error reporting and wow what a shocker I have probably thousands
I'm developing a web service (in asp.net) and I would like to have each
I have a form with file upload capabilities and I would like to be
I would like to make a web application (PHP) that will have overlay container
With error reporting on, or even for best practice, when unsetting a variable in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.