์˜ˆ์™ธ ์ฒ˜๋ฆฌ

์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ์จ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋งˆ์ฃผํ•  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ ์ƒํ™ฉ์— ๋Œ€ํ•ด ์ฝ”๋“œ๋ฅผ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฐ๊ณผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋ณด๋‹ค ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

์—๋Ÿฌ๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž.

int main(){
    vector<int> v{1, 2, 3, 4}
    int num;
    cin >> num;
    
    cout << v.at(num) << endl;
    
    return 0;
}
Output
10
=> Error

vector size๋ณด๋‹ค ํฐ index๋ฅผ ๋„ฃ์„ ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋žจ์ด ๋ฉˆ์ถฐ๋ฒ„๋ฆฐ๋‹ค.

์ด๋ ‡๊ฒŒ ๋  ๊ฒฝ์šฐ ๋งŒ๋“œ๋Š” ์‚ฌ๋žŒ ์ž…์žฅ์—์„œ๋Š” ์–ด๋Š ๋ถ€๋ถ„ ๋•Œ๋ฌธ์— ์—๋Ÿฌ๊ฐ€ ๋‚ฌ๋Š”์ง€ ์ฐพ๊ธฐ ์–ด๋ ต๋‹ค.

Try/catch

๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ๋ฌธ์„ ํ™œ์šฉํ•œ๋‹ค.

int main(){
    vector<int> v{1, 2, 3, 4};
    int num;
    cin >> num;
 
    try{
        cout << v.at(num) << endl;
 
    }
    catch(exception& e){
        cout << e.what() << endl;
        cout << "์ธ๋ฑ์Šค ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ!" << endl;
    }
    
    cout << "[program is still running!]" << endl;
 
    return 0;
}
 
Output1
์•Œ๊ณ  ์‹ถ์€ ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :3
4
[program is still running!]
Program ended with exit code: 0
Output2
์•Œ๊ณ  ์‹ถ์€ ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :10
vector
์ธ๋ฑ์Šค ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ!
[program is still running!]
Program ended with exit code: 0

์ธ๋ฑ์Šค๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฐ’์„ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, ์ด ์˜ค๋ฅ˜๋ฅผย ์บ์น˜ํ•ด์„œ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•ด ์ฃผ์—ˆ๋‹ค. ๋˜ํ•œ, ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ์„ ํ†ตํ•ด์„œ, ํ”„๋กœ๊ทธ๋žจ์ด ์ œ๋Œ€๋กœ ๋งˆ๋ฌด๋ฆฌ ๋˜์—ˆ๋‹ค๋Š” ์‚ฌ์‹ค์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค! ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๋ฅผ ํ†ต๊ณผํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ์—†์Œ์„ ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๋‹ค.

์‚ฌ์šฉ๋ฒ•

  1. ์˜ˆ์™ธ๋ฅผ throw ํ•  ๊ฒƒ ๊ฐ™์€ ๊ณณ์—ย tryย block์œผ๋กœ ์‹ผ๋‹ค.
  2. ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ ํ–ˆ์„ ๊ฒฝ์šฐ์—๋งŒ ๋ฐœ์ƒํ•  ์ฝ”๋“œ๋ฅผย catchย block ์œผ๋กœ ์‹ผ๋‹ค.
  3. exception& eย ๋Š” ํ”„๋กœ๊ทธ๋žจ์ด ๋˜์ง€๋Š” ์—๋Ÿฌ๋ฅผ ๋ฐ›์•„์ฃผ๋Š” ๊ฐ์ฒด์ด๋‹ค.
  4. try/catchย ๊ตฌ๋ฌธ์€ย ifย ๋ฌธ ์ฒ˜๋Ÿผ state๊ฐ€ ํ•œ์ค„์ด๋”๋ผ๋„ ๊ผญย {}ย ๋กœ ๊ฐ์‹ธ์ค˜์•ผ ํ•œ๋‹ค.

std::exception class

์˜ˆ์™ธ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ๊ธฐ๋ณธ c++์ œ๊ณต ํด๋ž˜์Šค์ด๋‹ค. ๋ชจ๋“  ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋Š” ์ด ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ์ž‘์„ฑ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ๋‹คํ˜•์„ฑ์˜ ์›๋ฆฌ์— ์˜ํ•ด ์–ด๋–ค ์—๋Ÿฌ๋ฅผ ๋˜์ง€๋”๋ผ๋„, ์ถ”์ƒํด๋ž˜์Šค์ธย exception classย ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

Member functions

์ด์ค‘์—์„œย what()ย ์ด๋ผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์ค‘์š”ํ•˜๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ์–ด๋–ค ์˜ˆ์™ธ์ด๋ƒ์— ๋”ฐ๋ผ ์ดย what()ย ํ•จ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๊ตฌ์„ฑ๋œ๋‹ค. ์ฆ‰, override ๋ฅผ ํ†ตํ•ด ์žฌ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค!

์—ฌ๊ธฐ์„œ๋Š” ์‚ฌ์‹ค ๋‹ค๋ฅธ๊ตฌ๋ฌธ์„ ์“ฐ์ง€๋งŒ ๊ทธ๊ฑด ๋ฐ‘์—์„œ ์•Œ์•„๋ณด์ž.

vector container ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

์œ„์˜ ์˜ˆ์—์„œ, vector์˜ index๋กœ ์ ‘๊ทผํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ฐ”๊ฟ”๋ณด์ž.

int main(){
    vector<int> v{1, 2, 3, 4};
    int num;
    cin >> num;
 
    try{
        // ์ด ๋ถ€๋ถ„!
        cout << v[num] << endl;
 
    }
    catch(exception& e){
        cout << e.what() << endl;
        cout << "์ธ๋ฑ์Šค ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ!" << endl;
    }
    
    cout << "[program is still running!]" << endl;
 
    return 0;
}
 
 
Output
์•Œ๊ณ  ์‹ถ์€ ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :3
4
[program is still running!]
Program ended with exit code: 0
์•Œ๊ณ  ์‹ถ์€ ์ธ๋ฑ์Šค๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :10
1
[program is still running!]
Program ended with exit code: 0

๊ธฐ์กด ์—๋Š” 10์„ ๋„ฃ์—ˆ์„ ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ ์ถœ๋ ฅํ–ˆ๋Š”๋ฐ, ์ด๋ฒˆ์—๋Š” ์—๋Ÿฌ๊ฐ€ ๋œจ์ง€ ์•Š๋Š”๋‹ค ์™œ ๊ทธ๋Ÿด๊นŒ?

์ด๊ฑด ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜๋Š”ย exceptionย ๊ฐ์ฒด๋ฅผย throwย ํ•ด์ฃผ์ง€ ์•Š์•„์„œ ๊ทธ๋ ‡๋‹ค.

ํด๋ž˜์Šค์—์„œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ ๋งŒ๋“ค๊ธฐ

์˜ˆ์™ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ

์œ„์—์„œ๋Š”ย try/catchย ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด vector container ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ๋ฐ›์•„์„œ ํ‘œํ˜„ํ•˜๋Š”์ง€ ์•Œ์•„๋ณด์•˜๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๋‚ด๊ฐ€ ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ, ์–ด๋–ค ๋ถ€๋ถ„์—์„œ ์—๋Ÿฌ๊ฐ€ ๋‚ ์ง€๋ฅผ ๊ทœ๋ช…ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ํ•„์š”ํ•˜๋‹ค.

์—ฌ๊ธฐ์„œ๋Š”, file์„ ์ฝ์–ด, ๋ฒกํ„ฐ๋กœ ๋„ฃ์–ด์ฃผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์•˜๋‹ค. ์ด๋•Œ ๋งŒ์•ฝ ํŒŒ์ผ์„ ์ฝ์ง€ ๋ชปํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ throw ํ•˜๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

throw ๊ตฌ๋ฌธ

vector<int> load_vector(string filename){
    ifstream fin(filename);
    if(!fin){
        // ์˜ˆ์™ธ ์ฒ˜๋ฆฌ throw
        throw FileNotFoundException(filename);
    }
 
    int num, value;
    vector<int> result;
 
    fin >> num;
    for(int i = 0; i < num; i++){
        fin >> value;
        result.push_back(value);
    }
 
    return result;
}

๊ตฌ๋ฌธํ•ด์„

  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ throw
  • ํŠน์ • ์ƒํ™ฉ์— ์—๋Ÿฌ๊ฐ€ ๋‚  ๊ฒฝ์šฐ ์šฐ๋ฆฌ๋Š” ์—๋Ÿฌ๋ฅผ ๋˜์ ธ์ค˜์•ผ ํ•œ๋‹ค.
  • ์ด๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ดย throwย ๊ตฌ๋ฌธ์ด๋‹ค.

์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค ๋งŒ๋“ค๊ธฐ

๊ทธ๋ ‡๋‹ค๋ฉดย FileNotFoundExceptionย ์€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค์ด๋ฏ€๋กœ,

์œ„์—์„œ ๋งํ–ˆ๋“ฏ, ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋Š”ย std::exceptionย ํด๋ž˜์Šค๋ฅผ ์ƒ์†ํ•ด์„œ ๋งŒ๋“ ๋‹ค.

class FileNotFoundException : public exception {
    string message;
public:
    // 1.
    FileNotFoundException(string _m): message("File not Found : " + _m){}
 
    // 2.
    virtual const char* what() const throw() {
        // 3.
        return message.c_str();
 
    }
};
 

๊ตฌ๋ฌธ ํ•ด์„

  1. ์ƒ์„ฑ์ž(constructor)
    • member variable ๋กœ ๊ฐ–๊ณ  ์žˆ๋Š”, message์— ๋‹ด์•„์ค€๋‹ค.
  2. what()ย override ํ•˜๊ธฐ.
    • virtual
      • ์ดย FileNotFoundExceptionย ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์จ์ฃผ์—ˆ๋‹ค.
    • const
      • what()ย ํ•จ์ˆ˜์˜ return type์€ย const char*ย ์ด๋‹ค.
      • ์ฆ‰, ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ ๊ฐ–๋Š”๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ,
      • ์ด ๋ฆฌํ„ด ๊ฐ’์€ ๋ณ€ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœย constย ๋ฅผ ์ ์–ด ๋ช…์‹œ์ ์œผ๋กœ ์จ์ค€๋‹ค.
    • what()
      • override๋ฅผ ์ˆ˜ํ–‰ํ•  ํ•จ์ˆ˜์˜ ์ด๋ฆ„์ด๋‹ค.
    • const
      • ์™ธ๋ถ€ ํ•จ์ˆ˜์—์„œ ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ,ย callbyreferenceย ๋กœ ๊ฐ€์ ธ์˜ฌ ๊ฒฝ์šฐ ๊ฐ’์„ ๋ณ€ํ™” ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.
      • ๊ทธ๋ž˜์„œ ์šฐ๋ฆฌ๋Š” void print(const ํด๋ž˜์Šค์ด๋ฆ„& p) ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ€์ ธ์˜ค๋Š”๋ฐ, ์ด๋•Œ ์ž…๋ ฅ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ๋ณ€ํ™”์‹œํ‚ค์ง€ ์•Š์œผ๋ฉด์„œ reference ํ•˜๊ฒ ๋‹ค๋Š” ์˜๋„์—์„œ const๋ฅผ ์ ๊ฒŒ๋˜๋ฉด, ๋‚ด๊ฐ€ ๋งŒ๋“  class ์—์„œ๋„ const๋ฅผ ์ ์€ ํ•จ์ˆ˜๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
      • ์ฆ‰, ์™ธ๋ถ€์—์„œ const ์ƒํƒœ๋กœ ์‚ฌ์šฉํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
    • throw()
      • ์œ„์—์„œ ์„ค๋ช…ํ•œ ์ด ๋ถ€๋ถ„์ด ๋‹ค๋ฅธ๋ฐ, exception ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์„ ๋•Œ,ย what()ย ํ•จ์ˆ˜๋ฅผย overrideย ํ• ๋•Œ๋Š”, throw()ย ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  3. string ์ธย messageย ๋ฅผย charย ๋กœ ๋งŒ๋“ค์–ด์ฃผ์ž.

main ์—์„œ try/catch ๊ตฌ๋ฌธ

์ด์ œ ํ•จ์ˆ˜๋„ ๋งŒ๋“ค๊ณ , ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํด๋ž˜์Šค๋„ ์ •์˜ํ–ˆ์œผ๋‹ˆ, try/catchย ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๋‚˜ํƒ€๋‚ด๋ณด์ž.

int main(){
 
    try{
        vector<int> v = load_vector("values.dat");
    }
    
// 1.   catch(FileNotFoundException& e)
    catch(exception& e){
        // 2.
        cout << e.what() << endl;
    }
 
    return 0;
}

๊ตฌ๋ฌธํ•ด์„

  1. ์ฃผ์„์ฒ˜๋ฆฌํ•œ ๋ถ€๋ถ„์„ ๋ณด๋ฉด, ์›๋ž˜ย catchย ๊ตฌ๋ฌธ์—์„œ๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„์ค˜์•ผ ํ•œ๋‹ค.
    • ์ฆ‰,ย catch(FileNotFoundException& e)ย ์ด๋Ÿฐ์‹์œผ๋กœ ์จ์ค˜์•ผ ํ•˜๋Š”๋ฐ, catch(exception& e)ย ์ด๋ ‡๊ฒŒ๋งŒ ์จ์คฌ๋‹ค.
    • ์ด ์ด์œ ๋Š” ์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ดย ๋‹คํ˜•์„ฑย ๋•Œ๋ฌธ์ด๋‹ค.
  2. ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ, ์ด ์—๋Ÿฌ๊ฐ€ ์–ด๋–ค ์—๋Ÿฌ์ธ์ง€ย what()ย ๋ฉ”์„œ๋“œ๋กœ ๋ณด์—ฌ์ค€๋‹ค.

์ „์ฒด ์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
 
using namespace std;
 
class FileNotFoundException : public exception {
    string message;
public:
    FileNotFoundException(string _m): message("File not Found : " + _m){}
 
    const char * what() const throw() {
        return message.c_str();
 
    }
};
 
 
vector<int> load_vector(string filename){
    ifstream fin(filename);
    if(!fin){
        throw FileNotFoundException(filename);
    }
 
    int num, value;
    vector<int> result;
 
    fin >> num;
    for(int i = 0; i < num; i++){
        fin >> value;
        result.push_back(value);
    }
 
    return result;
}
 
 
int main(){
 
    try{
        vector<int> v = load_vector("values.dat");
    }
    catch(exception& e){
        cout << e.what() << endl;
    }
 
    return 0;
}
 

try/catch ๊ตฌ๋ฌธ์˜ ์ž‘๋™ ๋ฐฉ์‹

Findint Nearestย catchย Statement

์—ฌ๋Ÿฌ๊ฐœ์˜ ํ•จ์ˆ˜์—ย exceptionย ๊ณผย try/catchย ๊ตฌ๋ฌธ์„ ๊ฑธ์–ด๋†“์„ ๊ฒฝ์šฐ, ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œย catchย ๊ตฌ๋ฌธ์„ ์ฐพ์•„๊ฐ€์„œ ์‹คํ–‰ํ•œ๋‹ค.

์˜ˆ์ œ

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
 
using namespace std;
 
// double throw
void filter(vector<int>& v, int i){
    v.at(i)++;
}
 
void compute(vector<int>& a){
    for(int i = 0; i < 6; i++){
        try{
            filter(a, i);
        }
        catch(exception& ex){
            cout << "******1๋ฒˆ catch********" << endl;
            cout << "๋ฐ˜๋ณต๋ฌธ์ด ์ค‘๊ฐ„์— ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค." << endl;
            cout << "when i =" << i << endl;
            cout << "**********************" << endl;
//            throw ex;
        }
    }
}
 
int main(){
    vector<int> list {10,20,30,40,50};
    try{
        compute(list);
    }
    catch(exception& e){
        cout << "******2๋ฒˆ catch******" << endl;
        cout << "์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค." << endl;
        cout << e.what() << endl;
        cout << "**********************" << endl;
    }
 
    cout << "ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ" << endl;
}
 
 
Output
******1๋ฒˆ catch********
๋ฐ˜๋ณต๋ฌธ์ด ์ค‘๊ฐ„์— ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค.
when i =5
**********************
ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
Program ended with exit code: 0

๊ตฌ๋ฌธ ํ•ด์„

  1. main ํ•จ์ˆ˜
    • main ํ•จ์ˆ˜๋ฅผ ๋ณด๊ฒŒ ๋˜๋ฉด, compute ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋œ๋‹ค.
  2. compute ํ•จ์ˆ˜
    • compute ํ•จ์ˆ˜๋Š” filter ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  3. filter ํ•จ์ˆ˜
    • filter ํ•จ์ˆ˜๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋„์ค‘, i์˜ ๊ฐ’์ด size๋ฅผ ๋„˜๋Š” ์ˆœ๊ฐ„์ด ์˜ค๊ณ , exception ๊ฐ์ฒด๋ฅผ throw ํ•œ๋‹ค.
  4. ํ”„๋กœ๊ทธ๋žจ์€ ์™”๋˜ ๊ธธ์„ ๋˜๋Œ์•„๊ฐ€๋ฉฐ ๊ฐ€์žฅ ๊ทผ์ ‘ํ•œ catch ๋ฌธ์„ ์ฐพ๋Š”๋‹ค.
    • ์ด ๊ฒฝ์šฐ compute ํ•จ์ˆ˜์— ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด catch ๋ฌธ์„ ์‹คํ–‰ํ•œ๋‹ค.
  5. main ํ•จ์ˆ˜๋กœ ๋Œ์•„์˜ฌ ๋•Œ๋Š” throw๊ฐ€ ๋‹ค ํ•ด์†Œ๋๊ณ , ๋Œ์•„์˜ค๋Š” throw๊ฐ€ ์—†์œผ๋ฏ€๋กœ catch ํ•จ์ˆ˜๋Š” ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

double throw(Re-thorwing)

๊ทธ๋Ÿฌ๋ฉด, main์œผ๋กœ ๋Œ์•„์˜ฌ ๋•Œ๋„, ๊ฐ์ฒด๋ฅผ ๋˜์ ธ์ฃผ๋ฉด, ์—ฌ๋Ÿฌ๋ฒˆ์˜ catch ๊ตฌ๋ฌธ์„ ์‹คํ–‰ ์‹œํ‚ฌ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ?

์˜ˆ์ œ

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
 
using namespace std;
 
// double throw
void filter(vector<int>& v, int i){
    v.at(i)++;
}
 
void compute(vector<int>& a){
    for(int i = 0; i < 6; i++){
        try{
            filter(a, i);
        }
        catch(exception& ex){
            cout << "******1๋ฒˆ catch********" << endl;
            cout << "๋ฐ˜๋ณต๋ฌธ์ด ์ค‘๊ฐ„์— ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค." << endl;
            cout << "when i =" << i << endl;
            cout << "**********************" << endl;
            throw ex;
        }
    }
}
 
int main(){
    vector<int> list {10,20,30,40,50};
    try{
        compute(list);
    }
    catch(exception& e){
        cout << "******2๋ฒˆ catch******" << endl;
        cout << "์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค." << endl;
        cout << e.what() << endl;
        cout << "**********************" << endl;
    }
 
    cout << "ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ" << endl;
}
 
 
Output
******1๋ฒˆ catch********
๋ฐ˜๋ณต๋ฌธ์ด ์ค‘๊ฐ„์— ๋ฉˆ์ท„์Šต๋‹ˆ๋‹ค.
when i =5
**********************
******2๋ฒˆ catch******
์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.
std::exception
**********************
ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
Program ended with exit code: 0