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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:22:43+00:00 2026-05-29T08:22:43+00:00

I want to write a common error handler which will catch custom errors thrown

  • 0

I want to write a common error handler which will catch custom errors thrown on purpose at any instance of the code.

When I did throw new Error('sample') like in the following code

try {
    throw new Error({'hehe':'haha'});
    // throw new Error('hehe');
} catch(e) {
    alert(e);
    console.log(e);
}

Log shows in Firefox as Error: [object Object] and I couldn’t parse the object.

For the second throw the log shows as: Error: hehe

Whereas when I did

try {
    throw ({'hehe':'haha'});
} catch(e) {
    alert(e);
    console.log(e);
}

the console showed as: Object { hehe="haha"} in which I was able to access the error properties.

What is the difference?

Is the difference as seen in the code? Like string will be just passed as string and object as objects but the syntax will be different?

I haven’t explored throwing error object… I had done only throwing strings.

Is there any other way than the above two mentioned methods?

  • 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-05-29T08:22:44+00:00Added an answer on May 29, 2026 at 8:22 am

    The difference between ‘throw new Error’ and ‘throw someObject’ in javascript is that throw new Error wraps the error passed to it in the following format −

    { name: ‘Error’, message: ‘String you pass in the constructor’
    }

    The throw someObject will throw the object as is and will not allow any further code execution from the try block, ie same as throw new Error.

    Here is a good explanation about The Error object and throwing your own errors

    The Error Object

    Just what we can extract from it in an event of an error? The Error object in all browsers support the following two properties:

    • name: The name of the error, or more specifically, the name of the constructor function the error belongs to.

    • message: A description of the error, with this description varying depending on the browser.

    Six possible values can be returned by the name property, which as mentioned correspond to the names of the error’s constructors. They are:

    Error Name          Description
    
    EvalError           An error in the eval() function has occurred.
    
    RangeError          Out of range number value has occurred.
    
    ReferenceError      An illegal reference has occurred.
    
    SyntaxError         A syntax error within code inside the eval() function has occurred.
                        All other syntax errors are not caught by try/catch/finally, and will
                        trigger the default browser error message associated with the error. 
                        To catch actual syntax errors, you may use the onerror event.
    
    TypeError           An error in the expected variable type has occurred.
    
    URIError            An error when encoding or decoding the URI has occurred 
                       (ie: when calling encodeURI()).
    

    Throwing your own errors (exceptions)

    Instead of waiting for one of the 6 types of errors to occur before control is automatically transferred from the try block to the catch block, you can also explicitly throw your own exceptions to force that to happen on demand. This is great for creating your own definitions of what an error is and when control should be transferred to catch.

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

Sidebar

Related Questions

I want to write a common method which returns any model like product, sales,etc.
I want write a little code analyzer which parses nested structures and translates into
In basic Java, if I write code to solve a common problem and want
In C# with MVC, i want to write a common utility or class in
I want to write some JavaScript that will change the onmousedown of a div
i want to write common page for viewing data list or single object. In
I am currently have a small program in Common Lisp, which I want to
Problem: I want to write a process that will allow a user to take
I want to write a CSS selector which matches the first occurrence of an
I want to write a bash script which takes 2 arguments, a and b

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.