Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

C++ Tutorial

C++ HOME C++ Intro C++ Get Started C++ Syntax C++ Output C++ Comments C++ Variables C++ User Input C++ Data Types C++ Operators C++ Type Conversion C++ Strings C++ Math C++ Booleans C++ If...Else C++ Switch C++ While Loop C++ For Loop C++ Break/Continue C++ Arrays C++ Structures C++ Enums C++ References C++ Pointers C++ Memory Mgmt

C++ Functions

C++ Functions C++ Function Parameters C++ Function Overloading C++ Scope C++ Recursion C++ Lambda

C++ Classes

C++ OOP C++ Classes/Objects C++ Class Methods C++ Constructors C++ Destructors C++ The this Pointer C++ Access Specifiers C++ Encapsulation C++ Friend Functions C++ Inheritance C++ Polymorphism C++ Operator Overload C++ Templates

C++ Files

C++ Files

C++ Date

C++ Date

C++ Errors

C++ Errors C++ Debugging C++ Exceptions C++ Input Validation

C++ Problem Solving

C++ Problem Solving C++ Build the Logic C++ Test Cases

C++ Data Structures

C++ Data Structures & STL C++ Vectors C++ List C++ Stacks C++ Queues C++ Deque C++ Sets C++ Maps C++ Unordered Map C++ Unordered Set C++ Iterators C++ Algorithms

C++ Namespaces

C++ Namespaces

C++ Projects

C++ Projects

C++ Cert

C++ Certificate

C++ How To

C++ How Tos

C++ Examples

C++ Examples C++ Real-Life Examples C++ Compiler C++ Exercises C++ Quiz C++ Code Challenges C++ Practice Problems C++ Syllabus C++ Study Plan

C++ Reference

C++ Reference C++ Keywords C++ <algorithm> C++ <cctype> C++ <cmath> C++ <cstdlib> C++ <cstring> C++ <ctime> C++ <deque> C++ <fstream> C++ <iomanip> C++ <iostream> C++ <list> C++ <map> C++ <memory> C++ <queue> C++ <random> C++ <set> C++ <sstream> C++ <stack> C++ <stdexcept> C++ <string> C++ <unordered_map> C++ <unordered_set> C++ <vector>


C++ stdexcept Library


Share

C++ stdexcept Classes

The <stdexcept> library has classes for common errors (exceptions). When something goes wrong, you throw an object of one of these classes, with a message that describes the error. Then you catch it, and use what() to get the message. The classes are in two groups:

  • Logic errors are mistakes in the program itself, like using an index that is too big, or a value that breaks a rule. You can find them by checking the values before you use them.
  • Runtime errors are problems that happen while the program runs, often because of something outside the program, like a missing file, or a result that is too big to store.

All the classes come from the exception class in the <exception> header. Catch exception to handle all of them in one place. Always catch an exception by reference (with &).

Logic Errors

Name Description
logic_error Reports a mistake in the logic of the program. It is the parent class of all logic errors
domain_error Reports that a value is outside the values that a math function works for
invalid_argument Reports that a value is not valid, like a negative age
length_error Reports that something would get longer than its maximum allowed length
out_of_range Reports that a value, like an index, is outside the allowed range

Runtime Errors

Name Description
runtime_error Reports a problem that happens while the program runs. It is the parent class of all runtime errors
overflow_error Reports that the result of a calculation is too big for its data type
range_error Reports that a result is outside the range of values that its data type can store
underflow_error Reports that the result of a calculation is too small (too close to 0) for its data type

Base Class (from <exception>)

Name Description
exception The parent class of all standard exceptions. Catch it to handle all of them in one place
what() Returns the message of an exception


Related Pages

C++ Exceptions Tutorial

C++ Errors Tutorial


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.