SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Node.js 
Exception Handling 
Do it the right way 
Minh Hoang - http://minhhoang.de
Node.js 
• is a still a relatively new programming language 
• Node.js crashes for any ‘uncatch error’ 
• Most developers are confused when to use 
• try/catch 
• pass error to a callback 
• throw an error
error vs. exception 
• an error = an instance of the Error class 
• can be created 
• can be thrown 
• can passed to callback 
• a thrown error = an Exception
errors 
Operational Errors 
(run-time issue, not bug) 
Programmer Errors 
(bug) 
• out-of-memory 
• connection issues 
• timeout 
• server return 500 HTTP code 
• socket hang-up 
• no ‘undefined’ check 
• no callback for asynchronous 
function 
• object type error 
a correct code must be able to deal with operation error
Best Practices for dealing 
with operation errors 
• Always put asynchronous code into try/catch (otherwise Node.js 
crashes if uncaught error happens) 
• Synchronous code: return error. 
• ENONT error why trying to open a file: create file the first before read it 
• socket hang-up: write code to reconnect if needed 
• ready to deal with invalid JSON - using try/catch 
• retry with 5xx HTTP error 
• no idea how to deal with unthinkable/-solvable issue - such as 
ENOMEM: log the error and crash (intentionally)
Dealing with programmer 
errors 
• crash immediately 
• how: throw new Error(‘ehh’); 
…. if you have a restarter in place (like Bluemix) 
…. or just use: forever.js
• instead of ‘callback(result)’ use ‘callback(err, 
result)’ 
• because: either ‘result’ or ‘err’ is NULL!
Error class 
• has {name, message, stack} 
• if you create an Error instance manually, at least 
provide {name, message}
last chance… 
• use process.on(‘uncaughtException’, function() 
{<do_sth>}); —> Global Catch 
• it’s NOT recommended…but what if your app’s 
just crashed without leaving a trace?
Automated Test 
• Crucially important 
• start with unit-test for all modules 
• then integration test 
• no test, no CI, you are in the coding hell!
and … think about this
Function 
- write it the right way - 
• first argument ist error 
• last ist callback 
• use return (return callback(null, data); or 
return(err)
Sources 
• Error Handling in Node.js https:// 
www.joyent.com/developers/node/design/errors 
• Node.js Best Practices: http:// 
www.slideshare.net/the_undefined/nodejs-best-practices- 
10428790 
• Continuous Development: http:// 
blog.risingstack.com/continuous-deployment-of-node- 
js-applications/
“Either I will find a way, 
or I will make one” 
–Philip Sidney

More Related Content

PPT
Image pre processing - local processing
DOC
Membuat robot line follower psrits
PPT
Street light controllercircuit.ppt 3
DOCX
Smart Helmet
PPT
Image sensor
DOCX
Project report on the Digital clock using RTC and microcontroller 8051
PDF
Image pre processing - local processing
Membuat robot line follower psrits
Street light controllercircuit.ppt 3
Smart Helmet
Image sensor
Project report on the Digital clock using RTC and microcontroller 8051

What's hot (9)

PPTX
TEAM SKYLINE Final ppt for snmart india hackathon
DOCX
B. Tech. Minor Project Synopsis
PPT
Safety app for woman
PPTX
smart helmet
PPTX
Light sensors seminar
PPTX
INFRARED SENSOR WORKING PRINCIPLE AND CIRCUIT
PDF
Digital Image Processing Notes - Akshansh
PPTX
House Price Prediction.pptx
PPTX
gray level transformation unit 3(image processing))
TEAM SKYLINE Final ppt for snmart india hackathon
B. Tech. Minor Project Synopsis
Safety app for woman
smart helmet
Light sensors seminar
INFRARED SENSOR WORKING PRINCIPLE AND CIRCUIT
Digital Image Processing Notes - Akshansh
House Price Prediction.pptx
gray level transformation unit 3(image processing))
Ad

Viewers also liked (20)

PDF
Enterprise JavaScript Error Handling (Ajax Experience 2008)
PPTX
PPTX
Macteam may2015 pt v7
DOCX
Kefolonia Website Mock
PPTX
Media A2 Evaluation Question 2
PDF
Making Mad Men more Agile | Agile on the beach | Sept 2015
DOCX
Case de mecânica
PDF
Circolare n 3 fondo di garanzia
PPT
Veneto turismo
PPTX
Powerpoint 1
PDF
創業管理:兼職創業者完整課程 Week 3
PDF
SEB wireframes
PDF
Презентация проекта Дождь Lite
PPTX
Perencanaan parkir jts
PPTX
Catering Eventos
PPSX
Wishclubteamusa_russia
PDF
New Orleans Saints and Academy Sports + Outdoors announce renewal of long-ter...
PPTX
I lama i_domyhomework_presentation
PPT
Croazia turismo
PPT
Francia PACA
Enterprise JavaScript Error Handling (Ajax Experience 2008)
Macteam may2015 pt v7
Kefolonia Website Mock
Media A2 Evaluation Question 2
Making Mad Men more Agile | Agile on the beach | Sept 2015
Case de mecânica
Circolare n 3 fondo di garanzia
Veneto turismo
Powerpoint 1
創業管理:兼職創業者完整課程 Week 3
SEB wireframes
Презентация проекта Дождь Lite
Perencanaan parkir jts
Catering Eventos
Wishclubteamusa_russia
New Orleans Saints and Academy Sports + Outdoors announce renewal of long-ter...
I lama i_domyhomework_presentation
Croazia turismo
Francia PACA
Ad

Similar to Node.js exception handling (20)

PDF
To Err Is Human
PPTX
Node.JS error handling best practices
PDF
Common mistake in nodejs
PDF
Perfomatix - NodeJS Coding Standards
PPTX
node.js errors
PDF
Towards 100% uptime with node
PDF
Error handling
PDF
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
PPTX
Lightning Talk: JavaScript Error Handling
PPTX
Node.js Getting Started &amd Best Practices
PPTX
Nodejs web service for starters
PDF
8 tips for mastering node.js
PDF
8 tips for mastering node.js
PDF
Node.js Course 1 of 2 - Introduction and first steps
PDF
ConFoo Presentation - Front-End Architecture
PDF
Exception+Logging=Diagnostics 2011
PDF
Exception Handling_ Mastering the Art of Dealing with Errors and Exceptions.pdf
PDF
9 anti-patterns for node.js teams
KEY
Node.js - Best practices
PPTX
Bucks County Tech Meetup: node.js introduction
To Err Is Human
Node.JS error handling best practices
Common mistake in nodejs
Perfomatix - NodeJS Coding Standards
node.js errors
Towards 100% uptime with node
Error handling
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
Lightning Talk: JavaScript Error Handling
Node.js Getting Started &amd Best Practices
Nodejs web service for starters
8 tips for mastering node.js
8 tips for mastering node.js
Node.js Course 1 of 2 - Introduction and first steps
ConFoo Presentation - Front-End Architecture
Exception+Logging=Diagnostics 2011
Exception Handling_ Mastering the Art of Dealing with Errors and Exceptions.pdf
9 anti-patterns for node.js teams
Node.js - Best practices
Bucks County Tech Meetup: node.js introduction

Recently uploaded (20)

PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
top salesforce developer skills in 2025.pdf
PDF
medical staffing services at VALiNTRY
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Digital Strategies for Manufacturing Companies
PPTX
Essential Infomation Tech presentation.pptx
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
System and Network Administration Chapter 2
PDF
AI in Product Development-omnex systems
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
wealthsignaloriginal-com-DS-text-... (1).pdf
Operating system designcfffgfgggggggvggggggggg
VVF-Customer-Presentation2025-Ver1.9.pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
top salesforce developer skills in 2025.pdf
medical staffing services at VALiNTRY
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Digital Strategies for Manufacturing Companies
Essential Infomation Tech presentation.pptx
Understanding Forklifts - TECH EHS Solution
Design an Analysis of Algorithms II-SECS-1021-03
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Which alternative to Crystal Reports is best for small or large businesses.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Internet Downloader Manager (IDM) Crack 6.42 Build 41
System and Network Administration Chapter 2
AI in Product Development-omnex systems
Upgrade and Innovation Strategies for SAP ERP Customers

Node.js exception handling

  • 1. Node.js Exception Handling Do it the right way Minh Hoang - http://minhhoang.de
  • 2. Node.js • is a still a relatively new programming language • Node.js crashes for any ‘uncatch error’ • Most developers are confused when to use • try/catch • pass error to a callback • throw an error
  • 3. error vs. exception • an error = an instance of the Error class • can be created • can be thrown • can passed to callback • a thrown error = an Exception
  • 4. errors Operational Errors (run-time issue, not bug) Programmer Errors (bug) • out-of-memory • connection issues • timeout • server return 500 HTTP code • socket hang-up • no ‘undefined’ check • no callback for asynchronous function • object type error a correct code must be able to deal with operation error
  • 5. Best Practices for dealing with operation errors • Always put asynchronous code into try/catch (otherwise Node.js crashes if uncaught error happens) • Synchronous code: return error. • ENONT error why trying to open a file: create file the first before read it • socket hang-up: write code to reconnect if needed • ready to deal with invalid JSON - using try/catch • retry with 5xx HTTP error • no idea how to deal with unthinkable/-solvable issue - such as ENOMEM: log the error and crash (intentionally)
  • 6. Dealing with programmer errors • crash immediately • how: throw new Error(‘ehh’); …. if you have a restarter in place (like Bluemix) …. or just use: forever.js
  • 7. • instead of ‘callback(result)’ use ‘callback(err, result)’ • because: either ‘result’ or ‘err’ is NULL!
  • 8. Error class • has {name, message, stack} • if you create an Error instance manually, at least provide {name, message}
  • 9. last chance… • use process.on(‘uncaughtException’, function() {<do_sth>}); —> Global Catch • it’s NOT recommended…but what if your app’s just crashed without leaving a trace?
  • 10. Automated Test • Crucially important • start with unit-test for all modules • then integration test • no test, no CI, you are in the coding hell!
  • 11. and … think about this
  • 12. Function - write it the right way - • first argument ist error • last ist callback • use return (return callback(null, data); or return(err)
  • 13. Sources • Error Handling in Node.js https:// www.joyent.com/developers/node/design/errors • Node.js Best Practices: http:// www.slideshare.net/the_undefined/nodejs-best-practices- 10428790 • Continuous Development: http:// blog.risingstack.com/continuous-deployment-of-node- js-applications/
  • 14. “Either I will find a way, or I will make one” –Philip Sidney