#include <iostream>
using namespace std;
void f(int test)
{
try{
if(test)
throw test; // int를 던짐
else
throw "Value is zero"; // char형 pointer를 던짐
}
catch(int i) {
cout << "Caught One! Ex. #: " << i << '\n';
}
catch(char *str) {
cout << "Caught a string: ";
cout << str << '\n';
}
}
int main()
{
cout << "start\n";
f(1);
f(2);
f(0);
f(3);
cout << "end";
return 0;
}
코드는 이건데 f(0)요기서는 왜
"Caught a string:Value is zero "
이문장이 출력됨??? 0도 int값아님??
using namespace std;
void f(int test)
{
try{
if(test)
throw test; // int를 던짐
else
throw "Value is zero"; // char형 pointer를 던짐
}
catch(int i) {
cout << "Caught One! Ex. #: " << i << '\n';
}
catch(char *str) {
cout << "Caught a string: ";
cout << str << '\n';
}
}
int main()
{
cout << "start\n";
f(1);
f(2);
f(0);
f(3);
cout << "end";
return 0;
}
코드는 이건데 f(0)요기서는 왜
"Caught a string:Value is zero "
이문장이 출력됨??? 0도 int값아님??
throw 했으니까 catch 해줘야지 야구모르냐
내말은 f(0)에서 왜 Caught One! Ex. #: 0 이렇게 안나오냐지...ㅠㅠㅠㅠ
c++의 에러처리를 공부하고 오면 알텐뎅
저 함수에서 보면 if(test)인 경우에 에러를 던지니까, f(0)인 경우에 test==0이고 0은 논리적으로 false니까 당연히 에러를 던지겠고
catch는 throw를 해주는 부분에서 잡아줘야되지않을까 try{ f(0)} catch(string err){ cout<<err<<endl; } 이런시긍로
아항 0을 논리적으로 봐야하는구나...ㄷㄷㄷ 고맙 주석없음 형
아쉽군 형이라니 오빠를 바랬는데