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

  • C++ Fundamentals
  • Antonio Mallia Francesco Zoffoli
  • 522字
  • 2021-06-11 13:35:58

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.

主站蜘蛛池模板: 汤阴县| 石棉县| 公安县| 酉阳| 常山县| 丹东市| 龙泉市| 太保市| 东城区| 临湘市| 子洲县| 格尔木市| 瑞安市| 巴南区| 乡城县| 长岭县| 长寿区| 思茅市| 泰州市| 隆安县| 垦利县| 阿拉尔市| 通榆县| 中牟县| 日照市| 澎湖县| 即墨市| 水城县| 启东市| 渑池县| 乐至县| 比如县| 栾川县| 棋牌| 孟连| 株洲县| 苗栗县| 福清市| 广宁县| 龙川县| 含山县|