C++ 예외 처리에 대해서 질문이~~

studioego의 이미지

winapi.co.kr에 있는 질답문 보고 갑자기 궁금해져서 질문하는데요
C++에서 예외처리를 할때 throw를 했을때 그것을 받는 catch가 없다면 널포인터 에러처럼 치명적 에러인가요??

#include <iostream>
using namespace std;

void print()
{
int a = 1;
if( a != 1 )
cout << "a" << "\n";
else
throw 1;
}

int main()
{
print();

return 0;
}

G++로 돌려보면
이렇게 Abort에러가 뜹니다.

[sungdh86@pcrc ~/clang]$ g++ catch.cpp
[sungdh86@pcrc ~/clang]$ ./a.out
terminate called after throwing an instance of 'i'
Abort
[sungdh86@pcrc ~/clang]$

throw을 할때 catch가 없으면 치명적인 오류가 왜 뜨는지 궁금하네요^^

ixevexi의 이미지

책에는 이렇게 쓰여 있네요
If an exception is thrown but not caught, the funcion std::terminate() will be called.

첨부하자면
set_unexpected함수를 통해
디폴트 _unexpected_handler함수를
자신의 void f(void)형 함수로 교체할 수 있으며

VS계열에서는 지원이 안된다고 나와있습니다.

C++, 그리고 C++....
죽어도 C++

doldori의 이미지

ixevexi wrote:
첨부하자면
set_unexpected함수를 통해
디폴트 _unexpected_handler함수를
자신의 void f(void)형 함수로 교체할 수 있으며

이 자체로는 맞는 말씀인데 set_unexpected()는 예외 명세(exception specification)와
관련된 것이고, uncaught exception과는 별로 관계가 없습니다.

uncaught exception과 set_terminate()의 관계는
unexpected exception과 set_unexpected()의 관계와 같습니다.

pynoos의 이미지

"throw한 것을 누군가 받지 않는다면 치명적인 오류가 발생한다"라는 말을 거꾸로 생각하면, throw 한 것을 누군가 받지 않는다면 무시하고 지나간다 정도인데, 어디로 지나가죠?

throw라는 개념은 스택을 한꺼번에 비우고 catch 문을 찾아 명령 실행 위치가 변경되는 행위입니다. catch가 없을 경우에 프로그램 바깥으로 날아가는 것이 정상적으로 보입니다만....

doldori의 이미지

pynoos wrote:
catch가 없을 경우에 프로그램 바깥으로 날아가는 것이 정상적으로 보입니다만....

예외 핸들러에 없는 예외가 발생했다는 것은 프로그램 설계 단계부터 고려하거나
예상하지 못한 상황이라는 뜻이죠. 어쩌겠습니까? gg 쳐야죠. ^^;
ixevexi의 이미지

doldori wrote:
ixevexi wrote:
첨부하자면
set_unexpected함수를 통해
디폴트 _unexpected_handler함수를
자신의 void f(void)형 함수로 교체할 수 있으며

이 자체로는 맞는 말씀인데 set_unexpected()는 예외 명세(exception specification)와
관련된 것이고, uncaught exception과는 별로 관계가 없습니다.

uncaught exception과 set_terminate()의 관계는
unexpected exception과 set_unexpected()의 관계와 같습니다.

제가 오해한 부분이 있었네요 ^^
uncaught 와 unexpected와의 차이점이 있었네요
오늘 또 하나 알아갑니다

C++, 그리고 C++....
죽어도 C++

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.