Giter Site home page Giter Site logo

exceptions's Introduction

Exceptions and Threads

Experimentation regarding what happens when exceptions are thrown (or not thrown) in different threading environments.

TL;DR

If an exception is raised in an threaded application, that thread will fail -- but other threads will continue.

In a single-threaded environment, handling these errors is trickier.

Why

Running a stack that includes a both multi-threaded and single-threaded (or psuedo-single-threaded) can have some tricky nuances. Do we catch uncaught exceptions? Do we allow the server to fallover? What are the the differences?

Examples

This repository contains a trivial example hitting Slow API to demonstrate I/O happening simultaneously.

.
├── README.md
├── node
│   ├── catch-uncaught-exception-sample.js
│   ├── standard-sample.js
│   └── throw-exception-sample.js
└── ruby
    ├── abort_on_exception_sample.rb
    └── standard_sample.rb

/ruby

$ ruby ruby/standard_sample.rb trivially shows us opening threads for each of the API requests. When the API request is for a duration of 2.0, an exception is raised.

Please notice that the other threads complete their execution prior to the exception being raised. The other two threads execute properly, this is great and something enjoy (see more in the Node example).

$ ruby ruby/abort_on_exception_sample.rb aborts on the exception to "mimic" more closely the Node.js examples below -- notice how when we abort on exception, the entire process is aborted when a single thread raises an exception.

Thread.abort_on_exception = true

/node

$ node node/standard-sample.js demonstrates the standard in Node.js: passing errors back through as the first argument of the callback.

if (delay === '2.0') return callback(DELIBERATE_ERROR, null);

$ node node/throw-exception-sample.js does exactly what it sounds like -- the exception is thrown rather than passed as an argument to the callback.

if (delay === '2.0') throw new Error(DELIBERATE_ERROR);

$ node node/catch-uncaught-exception-sample.js is the most peculiar of the bunch. Notice that the code now catches uncaught exceptions with

process.on('uncaughtException', function(err){
  console.log('Uncaught Exception:', err);
});

This effectively catches the thrown exception on Line 9 of the file. But notice that the first API request finishes -- the callbacks that had already been added to the stack continue, and anything afterwards are blown away. This trivial example demonstrates why using process.on('uncaughtException', ...) should be viewed as very dangerous.

Why? See how this leaks:

$ node node/leak.js

And in a separate process:

$ curl localhost:8000 # Request hangs

Contribute

Ask Why?! Challenge these ideas!

If you have another language or method of demonstrating exceptions -- please provide! Or better yet, illustrate something even further.

Pull requests welcome.

Resources

exceptions's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.