C++ Primer 第五章 Statements

5.3. Conditional Statements

5.3.2. The switch Statement

Switch statements

enum Color
{
    black,
    white
}color;
    switch (color)
    {
        case Color::black:
            std::cout << "Black";
            break;
        case Color::white:
            std::cout << "White";
            break;
        default:
            std::cout << "Unknown";
            break;
    }

The expression is converted to integral type.

The one restriction on this expression is that it must evaluate to an integral type (that is, char, short, int, long, long long, or enum). Floating point variables, strings, and other non-integral types may not be used here.

The result of the expression is compared with the value associated with each case, which is declared using the case keyword and followed by a constant expression.

5.4. Iterative Statements

5.4.3. Range for Statement

for (declaration : expression)
statement

expression must represent a sequence, such as a braced initializer list , an array, or an object of a type such as vector or string that has begin and end members that return iterators.

declaration defines a variable. It must be possible to convert each element of the sequence to the variable’s type.

vector<int> v = {0,1,2,3,4,5,6,7,8,9};
// range variable must be a reference so we can write to the elements
for (auto &r : v) // for each element in v
	r *= 2; // double the value of each element in v

5.6. try Blocks and Exception Handling

5.6.1. A throw Expression

// first check that the data are for the same item
if (item1.isbn() != item2.isbn())
	throw runtime_error("Data must refer to same ISBN");
// if we're still here, the ISBNs are the same
cout << item1 + item2 << endl;

Throwing an exception terminates the current function and transfers control to a handler that will know how to handle this error.

The type runtime_error is one of the standard library exception types and is defined in the stdexcept header.

5.6.2. The try Block

try {
program-statements
} catch (exception-declaration) {
handler-statements
} catch (exception-declaration) {
handler-statements
} // . . .

Once the catch finishes, execution continues with the statement immediately following the last catch clause of the try block.

If no appropriate catch is found, execution is transferred to a library function named terminate.

The behavior of that function is system dependent but is guaranteed to stop further execution of the program.

5.6.3. Standard Exceptions

C++ 异常处理

The exception header defines the most general kind of exception class named exception.

The stdexcept header defines several general-purpose exception classes.

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值