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 8056847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:50:32+00:00 2026-06-05T08:50:32+00:00

I have the following coffeescript code: try do something catch error log something throw

  • 0

I have the following coffeescript code:

try
   do something
catch error
   log something
   throw error

Should I use throw new Error(error) instead of throw error?

What is the difference?

  • 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-05T08:50:33+00:00Added an answer on June 5, 2026 at 8:50 am

    Same as in other languages like C# or Java:

    • throw error throws the same Error object
    • throw new Error(error) wraps it into a new Error object. The later is used, for example, in Java when you need to convert checked Exception into unchecked one. In JavaScript you don’t need to wrap exceptions as this would make stacktrace a bit longer and less pretty.

    Edit: There’re some security implications as well. Here’s an example:

    function noWrap() {
        try {
            var o = {}; o.nonexistingMethod();
        } catch (error) {
            throw error;
        }
    }
    
    function wrap() {
        try {
            var o = {}; o.nonexistingMethod();
        } catch (error) {
            throw new Error(error);
        }
    }
    

    Calling noWrap() produces the following error message:

    "TypeError: Object #<Object> has no method 'nonexistingMethod'"
    // with error.arguments === ['nonexistingMethod', o]
    

    Calling wrap() produces the following error message:

    "Error: TypeError: Object #<Object> has no method 'nonexistingMethod'"
    //  with error.arguments === undefined
    

    So, as you can see by using a wrapping Error object we can hide the arguments of original error. Suppose you’re writing one of the following:

    • some kind of a library
    • a script which will be loaded on a page that you don’t own (for example, some kind of like or tweet button)
    • a script on a page that has some third-party scripts loaded (social buttons, ads, tracking code, etc.)

    In all those cases listed above in order to stay secure you should wrap your Error objects. Otherwise you may accidentally leak references to your internal objects, functions and variables.

    Edit 2: Regarding stacktraces. Both variants preserve them. Here’s a working example and I get the following stacktraces in Chrome:

    // No wrapping:
    TypeError: Object #<Object> has no method 'nonexistingMethod'
        at noWrap (http://fiddle.jshell.net/listochkin/tJzCF/show/:22:23)
        at http://fiddle.jshell.net/listochkin/tJzCF/show/:37:5
        at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3901:62
        at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3915:20
    
    // Wrapping:
    Error: TypeError: Object #<Object> has no method 'nonexistingMethod'
        at wrap (http://fiddle.jshell.net/listochkin/tJzCF/show/:32:15)
        at http://fiddle.jshell.net/listochkin/tJzCF/show/:44:5
        at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3901:62
        at http://fiddle.jshell.net/js/lib/mootools-core-1.4.5-nocompat.js:3915:20 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm fairly new to using coffeescript and I have the following code eventBus =
I have the following CoffeeScript code: planet = new Planet p5, {x: 100, y:
I have the following CoffeeScript code: class Person secret = 0 constructor: (@name, @age,
I have the following code inside a class. (It's coffeescript-- and it's for a
I get the following error when I have coffeescript files in my assets/javascript folder
I have the following CoffeeScript code example: class TestClass constructor: () -> @list =
I have the following coffeescript code to generate and alert box: show_alert = ()
From the page file://localhost/Users/pistacchio/dev/epress/catflow/test_html/index.html I have the following (coffeescript) code trying to access an
I have the following coffeescript: class Vehicles constructor: (@name) -> class Car extends Vehicles
I have following Html file with javascript. This gives me error that testCircle is

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.