[C++] noexcept Keyword
์ ๋ฒ์ ์ ๋ฆฌํ๋ friend, explicit์ ์ด์ด noexcept๋ผ๋ ์๋ก์ด ํค์๋์ ๋ํด์ ์ ๋ฆฌํด๋ณด๋ ค ํฉ๋๋ค.
noexcept์ ์ด๋ค ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์๋,,, ๋ง ๊ทธ๋๋ก ํจ๊ป ์์ฑ๋ ํจ์์์ ์์ธ๋ฅผ ๋ฐ์์ํค์ง ์๊ฒ ๋ค๋ ๋ป์ ๊ฐ์ง๊ณ ์์ต๋๋ค.
ํด๋น ํค์๋๋ฅผ ์ฌ์ฉํ์ ๋ ์ด์ ์
1. ์ ์ ์ ์ปดํ์ผ๋ฌ์๊ฒ ํํธ๋ฅผ ์ค ์ ์์ต๋๋ค.
2. ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
3. ์ปดํ์ผ๋ฌ ์ต์ ํ๋ฅผ ๊ฐ๋ฅํ๊ฒ ๋ง๋ค์ด์ค๋๋ค.
์์ ์ ํจ๊ป ๋ณด๊ฒ ์ต๋๋ค.
#include <iostream>
#include <stdexcept>
using namespace std;
int funcA() noexcept {
// ...e
return 0;
}
int main() {
try {
funcA();
}catch (char c) {
std::cout << "Catch char!" << std::endl;
} catch (...) {
std::cout << "Default exception catch!" << std::endl;
}
std::cout << "Program ends without exception" << std::endl;
return 0;
}

funcA์์ ์๋ฌ๋ฅผ ๋์ง ์๋ ์์ง๋ง ์ ์์ ์ผ๋ก ์๋๋๋ ๋ชจ์ต์ ๋ณผ ์ ์์ต๋๋ค.
๊ทธ๋ฐ๋ฐ ๋ง์ฝ noexception ํค์๋๋ฅผ ์ฌ์ฉํ๋๋ฐ Exception์ throwํ๋ฉด ๋ฐ์ํ๋ฉด ์ด๋ป๊ฒ ๋ ๊น์
#include <iostream>
#include <stdexcept>
using namespace std;
int funcA() noexcept {
// ...
throw runtime_error("Exception!");
return 0;
}
int main() {
try {
funcA();
}catch (char c) {
std::cout << "Catch char!" << std::endl;
} catch (...) {
std::cout << "Default exception catch!" << std::endl;
}
std::cout << "Program ends without exception" << std::endl;
return 0;
}

libc++abi: terminating due to uncaught exception of type std::runtime_error: Exception!
ํด๋น ์ค๋ฅ๋ ์์ธ๊ฐ ์ฒ๋ฆฌ๋์ง ์๊ณ ํ๋ก๊ทธ๋จ์ด ๋น์ ์์ ์ผ๋ก ์ข ๋ฃ๋์๊ธฐ ๋๋ฌธ์ ๋ฌ๋ค๊ณ ํฉ๋๋ค.
noexception ๋๋ฌธ์ ์์ธ๊ฐ ์ ๋๋ก ์ฒ๋ฆฌ๋์ง ์์ ๊ฒ ๊ฐ๋ค์.