Khairuzzaman Mamun

  • About
  • Expeience
  • Education
  • Skills
  • Publications
  • w/R interest
  • SWOT Analysis
  • Hobbies
  • Connect me
July 11, 2025

What Are try and catch in C++?

Recently, I traveled to Nagoya on a business trip to deliver our camera-calibration software and train the client’s team. Once I arrived, I discovered that the application would occasionally terminate unexpectedly because an internal optimization routine diverged—usually triggered by low-quality images. Although I consider such failures a normal part of calibration, the abrupt crashes understandably left the customer confused. To prevent the software from halting during these divergence events, I wrapped the vulnerable function in a try–catch block. By catching the exception, the program can continue running smoothly even when optimization fails.

Exception handling in C++ enables you to detect errors at runtime and respond without crashing. The try block wraps code that might fail. The throw statement raises an error when something goes wrong. A matching catch block handles the error and lets the program continue gracefully.

Core Syntax:

  • try { … } Place code that could cause an error here.
  • throw value; Raise an exception. The value can be a number, a string, or an object.
  • catch (type name) { … } Handle exceptions of a specific type. The first matching catch is executed.
  • catch (...) { … } A catch-all handler for any exception type not already matched.

Step by Step Flow:

  1. Enter the try block.
  2. If no error occurs, skip all catch blocks and continue.
  3. If throw runs, leave the try block immediately.
  4. Search for the first catch whose parameter type matches the thrown value.
  5. Execute that catch block.
  6. Resume normal execution after the last catch.

Example Code:

#include <iostream>
#include <string>

int divide(int a, int b) {
if (b == 0) {
throw std::string(“Division by zero”);
}
return a / b;
}

int main() {
int x, y;
std::cout << “Enter dividend and divisor: “;
std::cin >> x >> y;

try {
int result = divide(x, y);
std::cout << “Result: ” << result << “\n”;
}
catch (const std::string& e) {
std::cout << “Error caught: ” << e << “\n”;
}
catch (…) {
std::cout << “An unexpected error occurred\n”;
}

std::cout << “Program continues…\n”;
return 0;
}

Key Points:

  • Wrap risky operations inside a try block.
  • Use throw to signal an error condition.
  • Catch the error in a catch block that matches the thrown type.
  • A final catch(...) can handle any unexpected exception.
  • After handling, the program continues past the catch blocks.

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Telegram (Opens in new window) Telegram
  • Share on WhatsApp (Opens in new window) WhatsApp
  • Share on X (Opens in new window) X
  • Print (Opens in new window) Print
  • Email a link to a friend (Opens in new window) Email
Like Loading…
ai
ai

Leave a comment Cancel reply

Menu

  • About
  • Expeience
  • Education
  • Skills
  • Publications
  • w/R interest
  • SWOT Analysis
  • Hobbies
  • Connect me

Contact

020-0127, Morioka, Iwate

Zenkunen, 3-9-3-305

presidentmamun@gmail.com

+81-80-9632-6849

Khairuzzaman Mamun

A multidisciplinary research engineer. To discover more about him—click here.

Designed by Khairuzzaman Mamun

  • Comment
  • Reblog
  • Subscribe Subscribed
    • Khairuzzaman Mamun
    • Already have a WordPress.com account? Log in now.
    • Khairuzzaman Mamun
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • View post in Reader
    • Manage subscriptions
    • Collapse this bar
%d