Share via

How does exception handling work in C++?

Gp Raji 0 Reputation points
2026-03-26T04:53:21.0733333+00:00

I am a beginner in Java and I want to understand exception handling clearly.

Can someone explain how try, catch, and finally blocks work with a simple example?

Also, what is the difference between checked and unchecked exceptions?

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

{count} votes

2 answers

Sort by: Most helpful
  1. Surya Amrutha Vaishnavi Lanka (INFOSYS LIMITED) 1,960 Reputation points Microsoft External Staff
    2026-03-27T05:51:18.6366667+00:00

    Thanks for reaching out!

    Exception handling allows C++ programs to:

    • Detect errors
    • Handle them gracefully
    • Prevent sudden program crashes

    Here is the simple code to learn more about exception handling

    int main() {

        try {

            int a = 10;

            int b = 0;

            if (b == 0) {

                throw b;  // throwing exception

            }

            cout << a / b << endl;

        }

        catch (int e) {

            cout << "Error: Division by zero is not allowed" << endl;

        }

        cout << "Program continues normally" << endl;

        return 0;

    }

    if there are any more concern feel free to reach out!

    0 comments No comments

  2. Castorix31 91,851 Reputation points
    2026-03-26T06:55:37.6433333+00:00
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.