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

  • Home
  • SEARCH
  • 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 81691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:29:59+00:00 2026-05-10T21:29:59+00:00

I’m writing a global error handling module for one of my applications. One of

  • 0

I’m writing a global error handling ‘module’ for one of my applications.

One of the features I want to have is to be able to easily wrap a function with a try{} catch{} block, so that all calls to that function will automatically have the error handling code that’ll call my global logging method. (To avoid polluting the code everywhere with try/catch blocks).

This is, however, slightly beyond my understanding of the low-level functioning of JavaScript, the .call and .apply methods, and the this keyword.

I wrote this code, based on Prototype’s Function.wrap method:

Object.extend(Function.prototype, {   TryCatchWrap: function() {     var __method = this;     return function() {             try { __method.apply(this, arguments) } catch(ex) { ErrorHandler.Exception(ex); }     }   } }); 

Which is used like this:

function DoSomething(a, b, c, d) {     document.write(a + b + c)     alert(1/e); }  var fn2 = DoSomething.TryCatchWrap(); fn2(1, 2, 3, 4); 

That code works perfectly. It prints out 6, and then calls my global error handler.

My question is: will this break something when the function I’m wrapping is within an object, and it uses the ‘this’ operator? I’m slightly worried since I’m calling .apply, passing something there, I’m afraid this may break something.

  • 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. 2026-05-10T21:29:59+00:00Added an answer on May 10, 2026 at 9:29 pm

    Personally instead of polluting builtin objects I would go with a decorator technique:

    var makeSafe = function(fn){   return function(){     try{       return fn.apply(this, arguments);     }catch(ex){       ErrorHandler.Exception(ex);     }   }; }; 

    You can use it like that:

    function fnOriginal(a){   console.log(1/a); };  var fn2 = makeSafe(fnOriginal); fn2(1); fn2(0); fn2('abracadabra!');  var obj = {   method1: function(x){ /* do something */ },   method2: function(x){ /* do something */ } };  obj.safeMethod1 = makeSafe(obj.method1); obj.method1(42);     // the original method obj.safeMethod1(42); // the 'safe' method  // let's override a method completely obj.method2 = makeSafe(obj.method2); 

    But if you do feel like modifying prototypes, you can write it like that:

    Function.prototype.TryCatchWrap = function(){   var fn = this; // because we call it on the function itself   // let's copy the rest from makeSafe()   return function(){     try{       return fn.apply(this, arguments);     }catch(ex){       ErrorHandler.Exception(ex);     }   }; }; 

    Obvious improvement will be to parameterize makeSafe() so you can specify what function to call in the catch block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the

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.