The Differences Between Exception Handling in C# and C++

C# and C++ handle exceptions in similar ways. The following are the differences between exception handling in C# and C++:

  • Exceptions in C# can only be represented by an instance of a class type that is derived from the System.Exception class. No such restrictions are placed on exceptions in C++.

    They can be represented by values of any type. For instance, a variable of integer type can represent an exception on C++.

  • The finally block in C# can contain termination code, which can be executed during normal and exceptional conditions. A similar operation in C++ can only be achieved at the cost of code duplication.

    Note

    C++ does not support the finally statement. If a block of code must be executed ...

Get Special Edition Using C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.