Coding⏱️ 2 min read📅 2026-05-31

How to Fix: Difference: std::runtime_error vs std::exception()

Understanding the difference between std::runtime_error and std::exception in C++.

Quick Answer: Use std::runtime_error for specific runtime errors that are not caught by std::exception, such as out-of-memory or division-by-zero errors.

The C++ Standard Library provides two classes, std::runtime_error and std::exception, to handle runtime errors. While they are both used for error handling, they differ in their purpose and usage.

🛑 Root Causes of the Error

  • Runtime errors are typically caused by invalid or unhandled input, unexpected conditions, or programming logic flaws.

🔧 Proven Troubleshooting Steps

Method 1: Throwing a std::runtime_error

  1. Step 1: Use the throw keyword to create an instance of std::runtime_error, passing a descriptive error message.

Method 2: Catching std::exception

  1. Step 1: Wrap your error handling code in a try-catch block.

✨ Wrapping Up

To summarize, use std::runtime_error when you need to indicate a specific error condition that occurs during runtime execution. On the other hand, use std::exception as a base class for all exceptions, including std::runtime_error, to provide a more general way of handling errors.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions