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

The Archive Base Latest Questions

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

Within node.js readFile() shows how to capture an error, however there is no comment

  • 0

Within node.js readFile() shows how to capture an error, however there is no comment for the readFileSync() function regarding error handling. As such, if I try to use readFileSync() when there is no file, I get the error Error: ENOENT, no such file or directory.

How do I capture the exception being thrown? The doco doesn’t state what exceptions are thrown, so I don’t know what exceptions I need to catch. I should note that I don’t like generic ‘catch every single possible exception’ style of try/catch statements. In this case I wish to catch the specific exception that occurs when the file doesn’t exist and I attempt to perform the readFileSync.

Please note that I’m performing sync functions only on start up before serving connection attempts, so comments that I shouldn’t be using sync functions are not required 🙂

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

    Basically, fs.readFileSync throws an error when a file is not found. This error is from the Error prototype and thrown using throw, hence the only way to catch is with a try / catch block:

    var fileContents;
    try {
      fileContents = fs.readFileSync('foo.bar');
    } catch (err) {
      // Here you get the error when the file was not found,
      // but you also get any other error
    }
    

    Unfortunately you can not detect which error has been thrown just by looking at its prototype chain:

    if (err instanceof Error)
    

    is the best you can do, and this will be true for most (if not all) errors. Hence I’d suggest you go with the code property and check its value:

    if (err.code === 'ENOENT') {
      console.log('File not found!');
    } else {
      throw err;
    }
    

    This way, you deal only with this specific error and re-throw all other errors.

    Alternatively, you can also access the error’s message property to verify the detailed error message, which in this case is:

    ENOENT, no such file or directory 'foo.bar'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to refer with an xsd:keyref from within a node/subnode structure to a
What options are there to handle unzipping .zip files from within a Node.js script
Within a Drupal theme, page.tpl.php, I'm setting a COOKIE/GLOBAL variable. However, when I try
Please tell me, are there limitations of maximum records per node within Basic license?
Need to define a seek(u,v) function, where u is the new node within the
I've been trying to invoke D3 within Node.js. I tried firstly to import d3.v2.js
Is is possible to display a node without children as a leaf node within
I need to find all instances of strings within an xml node. To be
Within my ajax call if an error is received I have an alert :
I'm having some problems creating a view for a node within my website. 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.