mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
see #12: translate ch09
This commit is contained in:
50
code/9/9.1.noexcept.cpp
Normal file
50
code/9/9.1.noexcept.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// 8.1.cpp
|
||||
// modern c++ tutorial
|
||||
//
|
||||
// created by changkun at changkun.de
|
||||
//
|
||||
// noexcept
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void may_throw() {
|
||||
throw true;
|
||||
}
|
||||
auto non_block_throw = []{
|
||||
may_throw();
|
||||
};
|
||||
void no_throw() noexcept {
|
||||
return;
|
||||
}
|
||||
|
||||
auto block_throw = []() noexcept {
|
||||
no_throw();
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::boolalpha
|
||||
<< "may_throw() noexcept? " << noexcept(may_throw()) << std::endl
|
||||
<< "no_throw() noexcept? " << noexcept(no_throw()) << std::endl
|
||||
<< "lmay_throw() noexcept? " << noexcept(non_block_throw()) << std::endl
|
||||
<< "lno_throw() noexcept? " << noexcept(block_throw()) << std::endl;
|
||||
|
||||
try {
|
||||
may_throw();
|
||||
} catch (...) {
|
||||
std::cout << "exception captured from my_throw()" << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
non_block_throw();
|
||||
} catch (...) {
|
||||
std::cout << "exception captured from non_block_throw()" << std::endl;
|
||||
}
|
||||
|
||||
try {
|
||||
block_throw();
|
||||
} catch (...) {
|
||||
std::cout << "exception captured from block_throw()" << std::endl;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user