try...catch
A statement that runs code in a try block and, if that code throws an exception, runs a catch block instead of crashing.
In more detail
Most languages, including JavaScript, TypeScript, Java and C#, provide it, often with a finally block that always runs. Catching lets a program recover or report the problem clearly; code should catch only errors it can actually handle.
Examples
The layer registry lesson wraps an illegal registration in try/catch and records that the layer inversion was rejected.In the browser: Aborted work, storage quota errors and invalid JSON all throw, so resilient code wraps them in try...catch and falls back sensibly.try { read(); } catch (IOException ex) { System.out.println(ex.getMessage()); }React error boundaries do not catch errors in event handlers or async code, so wrap those in try...catch yourself.Related terms
- exception — An error object a program raises to stop normal flow and report that something went wrong.
- async and await — Language keywords for writing code that waits for slow work, such as a network call, without blocking a thread.
- Error class — The JavaScript object that describes something going wrong, with a name such as TypeError and a human-readable message.
- throwIfAborted() — An AbortSignal method that throws the signal's abort reason if it has been aborted, and does nothing otherwise.
- checked exception — An exception type that the compiler forces every caller to either catch or declare with throws, making the failure part of the method's contract.
- exception translation — Catching a low-level exception at a boundary and throwing a higher-level one that fits the caller's vocabulary, keeping the original as the cause.
- teardown — Removing or releasing everything that was set up for a piece of work, from a test's open connections to a whole temporary environment, once it is no longer needed.
- error boundary — A class component that catches errors thrown while rendering its children and shows a fallback UI instead of crashing the whole app.
- assertion function — A function whose return type is asserts condition or asserts val is Type, which throws if the check fails and otherwise narrows the caller's code.
- never (type) — The TypeScript type with no values, used for code that can never be reached or functions that never return.
Taught in
- Design Systems Architecture: Foundations, Layers & Taxonomy — Design Systems & Component Engineering
- Async and Await: Managing Concurrency — Modern .NET Core, C# & Enterprise Microservices
- Cancellation With AbortController And Async Signal Propagation — Frontend Architecture & Web Platform
- Client Storage Paradigms: Cookies, Storage Tradeoffs — Frontend Architecture & Web Platform
- IndexedDB In Practice: Object Stores, Indexes And Transactions — Frontend Architecture & Web Platform
- Checked Versus Unchecked Exceptions — Modern Java: Virtual Threads & High-Throughput Services
- The Cost Of Checked Exceptions — Modern Java: Virtual Threads & High-Throughput Services
- Promotion, Wrappers And The Math API — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Switch, Loops And Labels — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Values And Flow — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Overloading And Varargs — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Inheritance, Overriding And Object's Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Interfaces: Default, Static And Private Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Checked, Unchecked, Catch And Finally — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Try-With-Resources And Suppressed Exceptions — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Exceptions, Arrays And Collections — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Lambdas, Functional Interfaces And Method References — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Modules: Exports, Opens And Services — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Jars, Automatic Modules And Runtime Images — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Platform And Virtual Threads, Runnable And Callable — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Thread Safety: Locks And Atomics — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Console And File I/O With Streams — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Paths And Files With NIO.2 — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Mock Exam: Sit It, Score It — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Final Review And Exam Day — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Stream Pipelines: pipeline, AbortSignal & Teardown — Node.js Internals & Backend Services
- Asynchronous File I/O: node:fs/promises & Handles — Node.js Internals & Backend Services
- Global Error Trapping: uncaughtException & Exits — Node.js Internals & Backend Services
- useActionState: Async Action Handlers And Pending States — React 19 & Modern Component Architecture
- useOptimistic: Instantaneous UI Updates with Rollback — React 19 & Modern Component Architecture
- Error Boundaries: componentDidCatch And Fallback UI Trees — React 19 & Modern Component Architecture
- any Versus unknown: Defensive Typing In Practice — Production TypeScript
- never: The Bottom Type And Exhaustive Switch Checking — Production TypeScript
- Type Aliases Versus Interfaces: Declaration Merging — Production TypeScript
- Typeof Guards, Truthiness And Equality Narrowing — Production TypeScript
- The in Operator And Instanceof Narrowing — Production TypeScript
- Assertion Signatures: asserts condition And Invariants — Production TypeScript
- Exhaustive Narrowing With never In Production Handlers — Production TypeScript
References
- MDN: try...catch
- Exception-handling statements
- Java Tutorials: The catch Blocks
- Java Language Specification, chapter 14: Blocks, Statements, and Patterns
- MDN: throw