🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #105

C++ Exceptions


Exceptions

As mentioned in the C++ Errors chapter, different types of errors can occur while running a program - such as coding mistakes, invalid input, or unexpected situations.

When an error occurs, C++ will normally stop and generate an error message. The technical term for this is: C++ will throw an exception (throw an error).


Exception Handling (try and catch)

Exception handling lets you catch and handle errors during runtime - so your program doesn't crash.

It uses three keywords:

  • try - defines the code to test
  • throw - triggers an exception
  • catch - handles the error

Example

try {
  // Code that may throw an exception
  throw 505;
}
catch (int errorCode) {
  cout << "Error occurred: " <<
errorCode;
}

Here, the program throws an exception with the value 505, which is caught and handled in the catch block.


Real-Life Example: Age Check

We can use exception handling to check if a user is old enough:

Example

try {
  int age = 15;
  if (age >= 18) {

cout << "Access granted - you are old enough.";
  } else {

throw (age);
  }
}
catch (int
myNum) {
  cout << "Access denied - You must be at least 18 years
old.\n";
  cout << "Age is: " << myNum;
}

Example explained

We use the try block to test some code: If the age variable is less than 18, we will throw an exception, and handle it in our catch block.

In the catch block, we catch the error and do something about it. The catch statement takes a parameter: in our example we use an int variable (myNum) (because we are throwing an exception of int type in the try block (age)), to output the value of age.

If no error occurs (e.g. if age is 20 instead of 15, meaning it will be be greater than 18), the catch block is skipped:

Example

int age = 20;

You can also use the throw keyword to output a reference number, like a custom error number/code for organizing purposes (505 in our example):

Example

try {
  int age = 15;
  if (age >= 18) {

cout << "Access granted - you are old enough.";
  } else {

throw 505;
  }
}
catch (int myNum) {

cout << "Access denied - You must be at least 18 years old.\n";

cout << "Error number: " << myNum;
}

Handle Any Type of Exceptions (...)

If you do not know the throw type used in the try block, you can use the "three dots" syntax (...) inside the catch block, which will handle any type of exception:

Example

try {
  int age = 15;
  if (age >= 18) {

cout << "Access granted - you are old enough.";
  } else {

throw 505;
  }
}
catch (...) {
  cout << "Access denied - You must be at least 18 years
old.\n";
}

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C++ Exceptions – FAQs

Quick answers about learning C++ Exceptions in C++.

This free note from CodingNow 2.0 explains C++ Exceptions in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including C++ Exceptions, is 100% free with no signup required.
With focused practice, most students grasp C++ Exceptions in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now