CHAPTER 25

image

Exception Handling

Exception handling allows programmers to deal with unexpected situations that may occur in a program.

Throwing exceptions

When a function encounters a situation that it cannot recover from it can generate an exception to signal the caller that the function has failed. This is done using the throw keyword followed by whatever it is the method wants to signal. When this statement is reached, the method will stop executing and the exception will propagate up to the caller where it can be caught, using a try-catch statement.

int divide(int x, int y){  if (y == 0) throw 0;  return x / y;}

Try-catch statement

The try-catch ...

Get C++ Quick Syntax Reference 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.