> For the complete documentation index, see [llms.txt](https://ezcode.gitbook.io/bnb-cpp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ezcode.gitbook.io/bnb-cpp/exception/undefined-1.md).

# 스택 풀기

## 스택 풀기 (Stack Unwinding) <a href="#ec-8a-a4-ed-83-9d-ed-92-80-ea-b8-b0-stack-unwinding" id="ec-8a-a4-ed-83-9d-ed-92-80-ea-b8-b0-stack-unwinding"></a>

어떤 함수를 호출 했는데 그 함수안에서 `throw`절이 실행되어 예외가 발생했다. 그런데 이 함수내에는 예외처리를 위한 try\~catch 블럭이 존재하지 않는다면 **예외처리는 그 함수를 호출한 영역으로 넘어가게 된다**.

```cpp
#include <iostream>
using namespace std;

void divide(int num1, int num2) {
  if (num2 == 0) {
    throw num2; // 예외 발생 
  }
  cout << "몫 : " << num1 / num2 << endl;
  cout << "나머지 : " << num1 % num2 << endl;
}

int main() {
  int num1, num2;

  cout << "두개의 수를 입력 : ";
  cin >> num1 >> num2;

  try { // 예외 상황이 발생할수 있는 부분
    divide(num1, num2);
    cout << "나눗셈 완료" << endl;
  }
  catch (int err) { // int 자료형을 받아서 처리한다.
    cout << "나누는 수는 " << err << "이 될 수 없습니다." << endl;
    cout << "프로그램을 다시 시작하세요." << endl;
  }
  cout << "프로그램을 종료합니다." << endl;
  return 0;
}
```

예외처리가 되지 않아서 함수를 호출한 영역으로 예외 데이터가 전달되는 현상을 "스택 풀기"라고 한다.

## 하나의 try블럭과 다수의 catch블럭 <a href="#ed-95-98-eb-82-98-ec-9d-98-try-eb-b8-94-eb-9f-ad-ea-b3-bc-eb-8b-a4-ec-88-98-ec-9d-98-catch-eb-b8-94" id="ed-95-98-eb-82-98-ec-9d-98-try-eb-b8-94-eb-9f-ad-ea-b3-bc-eb-8b-a4-ec-88-98-ec-9d-98-catch-eb-b8-94"></a>

```cpp
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

int StoI(char* str) {
  int len = strlen(str);
  int num = 0;

  if (len != 0 && str[0] == '0') {
    throw 0; // int 자료형 throw
  }

  for (int i = 0; i < len; i++) {
    if (str[i] <'0' || str[i]> '9') {
      throw str[i]; // char 자료형 throw
    }
    num += (int)(pow((double)10, (len - 1) - i) * (str[i] - '0'));
  }
  return num;
}

int main() {
  char str1[100];
  char str2[100];

  while (1) {
    cout << "두개의 숫자 입력 : ";
    cin >> str1 >> str2;

    try {
      int num1 = StoI(str1);
      int num2 = StoI(str2);
      cout << str1 << " + " << str2 << " = " << num1 + num2 << endl;
      break;
    }
    catch (int errNum) {
      cout << errNum << "으로 시작하는 숫자는 입력 불가" << endl;
    }
    catch (char errCh) {
      cout << "문자 " << errCh << "가 입력되었습니다." << endl;
    }
    cout << "재입력을 진행합니다." << endl << endl;
  }
  cout << "프로그램을 종료합니다." << endl;
  return 0;
}
```

### 모든 예외 검출 <a href="#eb-aa-a8-eb-93-a0-ec-98-88-ec-99-b8-ea-b2-80-ec-b6-9c" id="eb-aa-a8-eb-93-a0-ec-98-88-ec-99-b8-ea-b2-80-ec-b6-9c"></a>

catch의 매개변수를 엘립스(...) 으로 표현하면 모든 종류의 예외를 포착할 수 있다.

```cpp
try {
  // 예외가 발생할 가능성 있는 문장
}
catch(exptype1 e) {
  // exptype1 자료형 예외 처리
}
catch(exptype2 e) {
  // exptype2 자료형 예외 처리
}
catch(...) {
  // 위에서 처리가 안된 모든 유형의 예외 처리
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ezcode.gitbook.io/bnb-cpp/exception/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
