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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:24:29+00:00 2026-06-17T11:24:29+00:00

My node server dies when it is unable to parse JSON in the following

  • 0

My node server dies when it is unable to parse JSON in the following line:

var json = JSON.parse(message);

I read this thread on how to catch exceptions in node, but I am still not sure what is the proper way to wrap a try and catch block around this statement. My goal is to catch the exception and log an error to the console, and of course keep the server alive. Thank you.

  • 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-17T11:24:30+00:00Added an answer on June 17, 2026 at 11:24 am

    It’s all good! 🙂

    JSON.parse runs synchronous and does not know anything about an err parameter as is often used in Node.js. Hence, you have very simple behavior: If JSON parsing is fine, JSON.parse returns an object; if not, it throws an exception that you can catch with try / catch, just like this:

    webSocket.on('message', function (message) {
      var messageObject;
    
      try {
        messageObject = JSON.parse(message);
      } catch (e) {
        return console.error(e);
      }
    
      // At this point, messageObject contains your parsed message as an object.
    }
    

    That’s it! 🙂

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

Sidebar

Related Questions

On my node.js server I have the following code: //Generate a token var token
I have the following node.js server-side code: var app = require('http').createServer(handler) , io =
This is my node server var io = require('socket.io').listen(8889); io.sockets.on('connection', function (socket) { socket.emit('news',
I have the following node.js server set up listening to port 9001 var https
I have a strange problem. I do this query in a node.js server using
I'm trying to get a node.js server to send css files. I'm modifying this
Node.js server var http = require(http); http.createServer(function(request,response){ console.log(client connected); response.writeHeader(200,{Content-type: text/html}); response.write(Hello ;-)); response.end();
I have a Node server that does the following: I have a list of
The following code is written in a node server, using a monitoring module that
I'm building my node.js app which has the following structure: server.js controllers/user.js server.js require

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.