官术网_书友最值得收藏!

The try-catch block

During the execution of a program, an anomaly may occur. We refer to these runtime problems as exceptions, and they represent the response to an exceptional circumstance that arises outside of the normal functioning of a program. Designing code that's resilient to errors is one of the hardest things a programmer has to deal with.

Exceptions are generally thrown using the throw keyword when something that cannot be handled is encountered by the program. This is also referred to as raising an exception.

The try keyword is followed by a block containing statements that might throw one or more exceptions. These exceptions can be caught by one or more catch clauses, which are sequentially listed after the try block. The syntax for this is as follows:

try {

statement1;

} catch (exception-declaration1) {

statement2;

} catch (exception-declaration2) {

statement3;

}

...

A catch block consists of the catch keyword, the exception declaration, and a block. Based on the exception thrown inside the try block, one catch clause is selected and the corresponding block is executed. Once the catch block has terminated, the program continues its execution with the statement following the last catch clause.

Let's examine the following example to understand how try-catch conditional statements handle exceptions:

#include <iostream>

int main()

{

int x = 10;

try {

std::cout << "Inside try block" << std::endl;

if (x > 0) // True

{

throw x;// Following statement will be skipped

std::cout << "After throw keyword" << std::endl;

}

}

catch (int x ) {

std::cout << "Inside catch block: Exception found" << std::endl;

}

std::cout << "Outside try-catch block" << std::endl;

}

Output:

Inside try block

Inside catch block: Exception found

Outside try-catch block

Exercise 2: Counting the Number of Times a Specific Number Appears in a Given List

In this exercise, we will discuss using the if statement and a for loop to count our magic number. Here, we will be trying to find all numbers that are divisible by 3, ranging from 1 to 30.

Hint

To find out if a number is divisible by another, use the modulo (%) operator.

Now, let's perform the following steps:

  1. Import all the required header files:

    #include <iostream>

  2. We need to store the number of times a number is divisible by 3 in a counter. For this reason, we define and initialize the count variable to 0:

    unsigned count = 0;

  3. Now, we will use a for loop that produces values from 1 to 30 so that we can check whether they are divisible by 3 or not:

    for(unsigned x = 1; x <= 30; x++){

    }

  4. Finally, we will check in the body of the for loop by using an if statement and the expression x%3 == 0, which evaluates to true if the division has a remainder equal to 0:

    if (x%3 == 0) {

    count++;

    }

  5. If the previous condition returns to true, then the X variable is divisible by 3 and we can increment the counter.
  6. Finally, we can print count:

    std::cout << count << std::endl;

Bonus exercises:

  • Find how many numbers are divisible by 11 within the range of 1 to 100
  • Print all the numbers that are not divisible by 3 within the range of 1 to 30

Activity 1: Finding the Factors of 7 between 1 and 100 Using a while Loop

In the following activity, we will use a while loop and implement the previous concept from the earlier exercise to print the numbers between 1 and 100 that are divisible by 7.

Now, let's rewrite the previous code using a while loop in the following way:

  1. Create a variable of the unsigned type.
  2. Now, write the logic to print the numbers that are divisible by 7 using the while loop.
  3. Then, we have to increase the value of i after each iteration. Use the following code:

    i++;

    The solution for this activity can be found on page 282.

主站蜘蛛池模板: 余干县| 通道| 茶陵县| 喜德县| 榆林市| 巴楚县| 峨山| 南川市| 宜州市| 鄄城县| 怀安县| 西安市| 洛宁县| 正定县| 泰兴市| 东丰县| 册亨县| 安义县| 阜阳市| 昌黎县| 盖州市| 青川县| 驻马店市| 北安市| 石棉县| 深州市| 滦平县| 商都县| 乌拉特中旗| 阳原县| 珲春市| 天全县| 临高县| 水城县| 临沧市| 城市| 政和县| 瑞安市| 霍州市| 新沂市| 康保县|