100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Error Handling in Erlang

Erlang handles failure through a distinctive combination of try/catch exception handling and the 'let it crash' philosophy, where supervisors, not defensive code, are the primary safety net.

Data & RecordsIntermediate10 min readJul 10, 2026
Analogies

Three Kinds of Exceptions

Erlang has three distinct classes of exception, all raised and caught through the same mechanism but carrying different intent: error/1 signals a genuine bug or unexpected condition (like a failed pattern match, which itself raises a {badmatch, Value} error, or a badarith, badarg, or function_clause error the runtime generates automatically), exit/1 signals that a process wants to terminate, whether normally or abnormally, and throw/1 is a non-local return mechanism used for ordinary control flow, such as escaping early from a deeply nested computation.

🏏

Cricket analogy: A batter given out for hitting their own wicket (a genuine mistake, like error/1), a player retiring hurt mid-innings (a deliberate exit, like exit/1), and a captain calling for a strategic timeout to reset momentum (a planned interruption, like throw/1) are three distinctly different reasons play might stop, mirroring Erlang's three exception classes.

try...catch and the catch Operator

The try Expr of Pattern -> Body catch Class:Reason:Stacktrace -> Handler end construct is the modern (OTP 21+) way to both run code and handle any of the three exception classes it might raise, where Class is bound to error, exit, or throw so a single catch clause can distinguish which kind of exception occurred, and the optional Stacktrace variable, retrievable this way instead of the older erlang:get_stacktrace/0, lets you log or re-raise the original error context. The older, terser catch Expr operator is still common for simple cases: it evaluates Expr and, if an exception occurs, returns {'EXIT', Reason} for error and exit, or Reason directly for a throw, but because it collapses all three classes into ambiguous return shapes, catch is best reserved for situations where you genuinely don't care which kind of exception happened.

🏏

Cricket analogy: A stump microphone that separately tags an umpire's decision, a player's own comment, and crowd noise into three distinct labeled channels is like try...catch's Class:Reason:Stacktrace distinguishing error, exit, and throw, whereas an old single unlabeled broadcast feed that just says 'something happened' is like the terser catch operator's ambiguous {'EXIT', Reason} shape.

erlang
-module(error_demo).
-export([safe_divide/2, demo/0]).

safe_divide(A, B) ->
    try A / B of
        Result -> {ok, Result}
    catch
        error:badarith:Stacktrace ->
            {error, {badarith, Stacktrace}};
        error:badarg ->
            {error, badarg}
    end.

demo() ->
    {ok, 2.5} = safe_divide(5, 2),
    {error, {badarith, _}} = safe_divide(5, 0),

    %% "Let it crash": a worker that just dies on bad input, trusting a
    %% supervisor to restart it, instead of defensively handling every case.
    process_flag(trap_exit, true),
    Worker = spawn_link(fun() -> 1 = 2 end),
    receive
        {'EXIT', Worker, Reason} ->
            io:format("Worker crashed as expected: ~p~n", [Reason])
    after 1000 ->
        io:format("No exit received~n")
    end.

Since Erlang/OTP 21, the recommended catch form is Class:Reason:Stacktrace, which binds the stacktrace explicitly in the catch clause; the older erlang:get_stacktrace/0 function it replaced was error-prone because the stacktrace could be silently clobbered by intervening code before you read it.

The 'Let It Crash' Philosophy

Erlang's most distinctive error-handling idea is 'let it crash': rather than wrapping every risky operation in defensive try...catch blocks, idiomatic Erlang code often lets a process simply die when it hits an unexpected condition, trusting a supervisor to notice the crash and restart the process into a known-good state. This works because Erlang processes are cheap and isolated, a crash in one process cannot corrupt another process's memory, and restarting a process from scratch is usually both simpler and more reliable than trying to enumerate every possible failure mode and defensively code around each one, especially for transient failures like a database connection blip that a fresh process will simply not encounter on its next attempt.

🏏

Cricket analogy: A T20 franchise doesn't rebuild its entire strategy around one dismissal; it sends the next batter in fresh rather than trying to defend against every conceivable way a wicket could fall, the same 'restart rather than defend everything' philosophy 'let it crash' embodies by trusting a supervisor to restart a failed process cleanly.

Reach for try...catch only around operations whose failure you can meaningfully recover from right there (like a malformed external input you can reject with a clean error tuple), wrapping broad swaths of business logic in catch-all try...catch blocks defeats 'let it crash' and tends to hide bugs that a supervisor restart would have surfaced and fixed cleanly.

Linking, Monitoring, and Supervisors

Supervision trees are how 'let it crash' becomes a real fault-tolerance strategy rather than just chaos: a process can link/1 to another so that if either one crashes abnormally, the linked process receives an exit signal too (which, by default, kills it in turn, propagating the failure), or it can set process_flag(trap_exit, true) to convert those exit signals into ordinary messages it can inspect and react to instead of dying, this is precisely the mechanism a supervisor behavior uses internally to notice when a child process has crashed and restart it according to a configured strategy (one_for_one, one_for_all, rest_for_one), without the programmer having to hand-roll any of that detection and restart logic themselves.

🏏

Cricket analogy: A team's fielding chain reacts as one unit, if the wicketkeeper misses a signal the slip cordon adjusts automatically, mirroring how linked Erlang processes propagate a failure signal, while the vice-captain (like a trap_exit process) absorbs bad news calmly instead of reacting instinctively.

  • Erlang has three exception classes, error (bugs/unexpected conditions), exit (process termination), and throw (non-local control flow), all raised and caught through the same mechanism.
  • try...catch (with the modern Class:Reason:Stacktrace form) lets you handle each exception class distinctly; the older catch operator collapses them into an ambiguous {'EXIT', Reason} or bare Reason.
  • 'Let it crash' means idiomatic Erlang often skips defensive error handling in favor of letting a process die and letting a supervisor restart it into a known-good state.
  • This works because processes are cheap, isolated, and share no memory, so one process's crash cannot corrupt another's state.
  • link/1 propagates exit signals between linked processes by default; process_flag(trap_exit, true) converts those signals into ordinary messages a process can react to.
  • Supervisor behaviors use linking and trap_exit internally to detect a child's crash and restart it according to a configured strategy (one_for_one, one_for_all, rest_for_one).

Practice what you learned

Was this page helpful?

Topics covered

#Programming#ErlangStudyNotes#ErrorHandlingInErlang#Error#Handling#Erlang#Three#ErrorHandling#StudyNotes#SkillVeris

Frequently Asked Questions

21 categories · pick one to explore

Where can I get free study notes for programming and tech subjects?
SkillVeris offers completely free study notes covering programming and tech subjects, with no signup fees or paywalls. The notes are structured by course and topic, written for quick understanding, and enriched with the Learn Through Hobbies analogy method, so you can revise concepts through cricket, music, gaming, cooking and more.
Are SkillVeris study notes good for exam revision?
Yes, the study notes are designed for efficient revision: each topic answers its heading immediately, keeps explanations concise, and links to related glossary terms and cheat sheets. Students preparing for university exams or certification tests use them as quick revision notes because they distil concepts without the padding of full textbooks.
What subjects do the free study notes cover?
The study notes span the platform's main domains, including AI and machine learning, Python and programming, web development, DevOps, cloud, security and databases. Coverage mirrors the 37 live courses, so notes exist for the topics you are actually studying, and new note sets are added as courses launch.
How are SkillVeris study notes different from regular textbooks?
The notes are answer-first, concise and free, whereas textbooks are long and often expensive. Each section explains one concept directly, then reinforces it through selectable hobby analogies like cricket or cooking. Notes also cross-link to the glossary, blog and cheat sheets, letting you jump to related material instantly instead of flipping pages.
Can I use the developer study material without creating an account?
The study notes are free to access, and SkillVeris does not charge anything for its developer study material at any point. Browsing notes is straightforward from the Study Notes section, and if you want progress tracking, certificates and AI Mentor conversations tied to your learning, a free account unlocks those extras.
Do the study notes explain concepts with analogies?
Yes, this is a signature SkillVeris feature. Study notes use the Learn Through Hobbies method, explaining technical concepts through analogies from twelve domains including cricket, music, gaming, photography, travel, movies, fitness, chess, cooking, finance, business and sports. You can switch the analogy domain instantly to whichever hobby makes the concept click.
Are the revision notes suitable for last-minute exam preparation?
Yes, revision notes on SkillVeris work well for last-minute preparation because every section states the answer in its first sentences, so skimming is genuinely effective. Pair them with the relevant cheat sheet for formulas and syntax, and use the glossary for any unfamiliar term you meet while cramming.
Is there free study material for AI and machine learning?
Yes, SkillVeris provides free study notes across its AI and ML catalogue, covering Python for AI, deep learning frameworks like PyTorch and TensorFlow, Hugging Face Transformers, Large Language Models, RAG, AI agents and MLOps. All of it is free, making it a strong resource for Indian students and global learners alike.
Can beginners understand the study notes, or are they for experts?
Beginners can absolutely use them. The notes are written in plain language, define terms as they appear, and lean on hobby analogies to make abstract ideas concrete. Difficulty scales with the underlying course level, so beginner-course notes stay gentle while advanced-course notes go deeper, and the glossary supports you throughout.
How do study notes connect with SkillVeris courses?
Study notes are organised by course and topic, so they map directly to the structured courses and their 24–40-lesson curriculum. Many learners study a lesson first, then use the matching notes for revision before module assessments and the final exam, where 80 percent is required to pass and earn the certificate.
Are there study notes for Python specifically?
Yes, Python is well covered through notes tied to the Python-focused courses, including Python for AI and ML. Topics span fundamentals through applied machine learning usage. You can reinforce the notes with Python practice in Code Lab, which runs code in your browser with no installation required.
Do the study notes include code examples?
Yes, study notes include code examples wherever a concept is best shown in code, alongside explanations, key points and analogies. Reading a snippet in the notes and then reproducing it yourself in Code Lab is an effective loop, since Code Lab lets you run code in the browser across six languages.
How often is new study material added to SkillVeris?
Study material grows alongside the course catalogue. Whenever new courses join the platform's 37 live courses, matching study notes, glossary entries and cheat sheets are added so the resources stay in sync. Existing notes are also refined over time, so it is worth revisiting topics you studied earlier.
Can I use SkillVeris notes to prepare for technical interviews?
Yes, the notes make excellent interview revision because they compress each concept into direct, answer-first explanations, which mirrors how you should answer interview questions. Combine them with the SkillVeris interview questions feature, which includes readiness scoring, to test whether your revision has actually made you interview-ready.
Are the study notes mobile-friendly for studying on the go?
Yes, the study notes are built to load fast and read comfortably on mobile devices, so you can revise during a commute or between classes. Sections are short and answer-first, which suits small screens, and analogy switching works on mobile too, letting you study anywhere without carrying books.
What is the difference between study notes and cheat sheets?
Study notes explain concepts in depth with context, examples and analogies, making them ideal for learning and revision. Cheat sheets are compact quick-reference summaries of syntax, commands and key facts, ideal once you already understand a topic. Most learners study the notes first, then keep the cheat sheet handy while coding.
Do study notes help if I am stuck on a course lesson?
Yes, reading the matching study notes often clarifies a lesson because the same concept is explained from a different angle, frequently with a different analogy. If you are still stuck, ask the AI Mentor, which answers 24/7 at Quick, Detailed or Deep-dive depth until the idea genuinely makes sense.
Is there free study material for DevOps and cloud topics?
Yes, SkillVeris carries free study notes for DevOps and cloud topics as part of its coverage across 37 live courses. The material suits learners following the DevOps Engineer or Cloud Engineer paths, and it links to related glossary terms and cheat sheets so you can revise the whole toolchain in one place.
Can school or college students in India use these notes for projects?
Yes, students across India and worldwide use SkillVeris notes for coursework, projects and exam preparation, and everything is free, which matters for student budgets. The notes explain concepts clearly enough to cite in project reports, and Code Lab lets you prototype the project code directly in your browser.
How should I combine study notes with other SkillVeris resources?
A proven loop: learn from a course lesson, revise with the matching study notes, look up unfamiliar terms in the glossary, keep the cheat sheet open while practising in Code Lab, and quiz yourself with interview questions. The AI Mentor fills any remaining gaps 24/7, at whatever depth you need.

What Learners Say

Real journeys from the SkillVeris community — swipe for more.

SkillVeris taught me Python through Cricket. Now I’m building real projects and feeling confident!
Arjun S. · B.Tech Student
The best platform for hobby-based learning. Concepts finally stick.
Priya R. · Data Analyst
I went from zero coding to a portfolio of projects — all by learning through my love for gaming. Landed my first internship!
Kabir M. · CS Undergraduate
Trending Topics50 popular tags — tap to explore
Trending CoursesAll 37 free courses — tap to browse