
statement is used to alert the system that the exception has occurred. Associated with
each try clause is a catch clause. The body of the catch clause contains the exception
handler, the code to handle the exception. If an exception is not thrown, execution
continues with the statement immediately following the try-catch statement.
Let’s examine the following concrete example, which reads and sums positive
values from a file. An exception occurs if a zero or negative value is encountered. If
this happens, we want to write a message to the screen and stop the program.
string msg = "Negative value";
try
{
infile >> value;
do
{
if (value <= 0)
throw msg; // ...