올라온 질문이 없습니다!
수업 내용에 궁금한 점이있다면
질문을 작성해주세요!
14-2장에서 말씀하신 엄격함이 상속부분에서는 완화된건가요?
14-2장에서 throw -1을 int 로 double로 캐치할때는 안된다 하셨고 이것은 엄격함때문이라 하셨는데 상속구조에서 base가 아닌 derived 를 받는것도 엄격함으로 구분될줄 알았지만 derived를 catch한다해도 base가 들어오면 catch가 되는 것을 보아 상속구조에서는 엄격함이 완화가 된건가요?
오원택
2
3
[05:17] 상속을 사용할 때 주의가 필요한 경우(객체잘림)
안녕하세요?
05분17초에 상속을 사용할 때 주의가 필요한 경우를 설명해주시고 계신대요.
이 부분에서 갑자기 virtual 키워드가 떠올라서
Base class에 virtual을 붙여줬더니
class Exception
{
public:
virtual void report()
{
cerr << "Exception report" << endl;
}
};
class ArrayException : public Exception
{
public:
void report()
{
cerr << "Array exception" << endl;
}
};
// catch (ArrayException & e)
// {
// e.report();
// }
catch (Exception & e)
{
e.report();
}
받아주는 쪽에서 ArrayException을 받는 부분을 추가를 안해줘도 (주석 부분)
Derived class(ArrayException)의 report가 실행이 되더라고요.
이 부분이 이렇게도 가능한건가요?
만약 가능하다고 해도 사용안하는 게 좋은 거라면 그 이유는
12-2에서 virtual 키워드 설명해주실 때
virtual 키워드는 호출이 아주 빈번하게 되는 함수에는 쓰면 안좋다고 설명해주셨는데
try catch 예외처리는 문제가 생길 수 있는 부분에 대해 처리하는 부분인 만큼 자주 실행될 수 있는 부분이니까
virtual 키워드를 사용 안하는 게 좋은 건가요?
감사합니다.
<code>
// 14_3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
using namespace std;
class Exception
{
public:
virtual void report()
{
cerr << "Exception report" << endl;
}
};
class ArrayException : public Exception
{
public:
void report()
{
cerr << "Array exception" << endl;
}
};
class MyArray
{
private:
int m_data[5];
public:
int& operator [] (const int& index)
{
//if (index < 0 || index >= 5) throw - 1;
if (index < 0 || index >= 5) throw ArrayException();
return m_data[index];
}
};
void doSomething()
{
MyArray my_array;
try
{
my_array[100];
}
catch (const int& x)
{
cerr << "Exception " << x << endl;
}
// catch (ArrayException & e)
// {
// e.report();
// }
catch (Exception & e)
{
e.report();
}
}
int main()
{
doSomething();
// try
// {
// doSomething();
// }
// catch (Exception & e)
// {
// cout << "main()" << endl;
// e.report();
// }
// catch (ArrayException & e)
// {
// cout << "main()" << endl;
// e.report();
// }
return 0;
}
호두
1
2
bona kim님과 같은 질문입니다~!
Q)어떠한 이유로 main()의 첫 doSomething(); 유무에 따라 에러가 발생하는 건가요?
본 코드는 영상 (9:50)와 동일합니다. 단, main() 첫 줄에 doSomething()을 호출한 것만 다릅니다.
class Exception
{
public:
void report()
{
cerr << "Exception report" << endl;
}
};
class ArrayException : public Exception
{
public:
void report()
{
cerr << "Array exception" << endl;
}
};
class MyArray
{
private: int m_data[5];
public:
int& operator [] (int index)
{
if (index < 0 || index >= 5)
throw ArrayException();
return m_data[index];
}
};
void doSomething()
{
MyArray my_array;
try { my_array[100]; }
catch (ArrayException& e)
{
cout << "doSomething() " << endl;
throw e; // error break out!!
}
}
int main()
{
doSomething(); // only this line is different from before!!
try
{
doSomething();
}
catch (ArrayException& e)
{
cout << "main()" << endl;
e.report();
}
return 0;
}
위 코드는 사실상 doSomething에서 특정 대상을 throw할 필요없이 그냥 throw해도 main()의 catch가 받아줘야 하는 코드라고 생각해요.
test) throw e; vs throw; = 기능상 결과 똑같음(위 코드에 한에서만).
throw와 catch 순서 상 : class MyArray (throw) -> void doSomething (catch & throw) -> main (catch)
그런데 자꾸 런타임 에러가 발생합니다. 아래 사진, 또는 Unhandled exception 에러 발생.
(디버깅)본 에러는 doSomething의 throw이 작동하는 순간 발생합니다.
신기하게도 main()의 가장 첫 번째 doSomething();을 지워주면 이러한 문제가 발생하지 않고, 함수 doSomething안에 있는 throw도 main()의 catch까지 문제없이 전달됩니다.
LogThinker
0
5
질문입니다..!
14.3강의 10:03 보다가 질문드려요.
class Exception
{
public :
void report()
{cerr << "Exception report" << endl;}
};
class ArrayException : public Exception
{
public :
void report()
{cerr <<"Array exception" << endl;}
};
class MyArray
{
private : int m_data[5];
public :
int & operator [] (int index)
{
if(index < 0 || index >=5) throw ArrayException()}
}
void doSomething()
{
MyArray my_array;
try {my_array[100];}
catch(ArrayException & e)
{ cout << "doSomething() " << endl;
throw e; }
}
int main()
{
doSomething();
try
{ doSomething();}
catch (ArrayException & e)
{ cout << "main()" << endl;
e.report(); }
이렇게 doSomething()함수 안에 catch(ArrayException & e)이 정의되어 있는 상태에서 e를 다시 rethrow를 해주고 실행시키면 main()함수의 첫 번째 doSomething()이 실행되고 디버깅 에러가 발생합니다. 아마 e를 다시 예외처리해주지 못하는것 같은데,왜 main()함수에 정의 된 catch (ArrayException & e)로 예외처리를 하지 못하고 에러가 나는 건지 궁금해요
bona Kim
0
2
2:50 throw Exception()
2:50 경에 throw Exception()을 하는데 Exeption() 의 의미는 단순히 throw만을 하기위해 Exception 객체를 만들기 애매하니 익명Exception객체(임시객체)를 만들어 throw 하는것인가요?
TaeChoon Pakr
0
2
5:15 예외클래스의 전달 과 캐치
5:15 경에
예외클래스를 전달하고 캐치시에
catch(Exception & e ) -> Exception 클래스의 print 실행
catch(ArrayException & e)-> ArrayException 클래스의 print 실행
로 reference로 받고있는데 이러면 음 .. 강의에서 말씀하시는것처럼 ' 객체잘림 '이라기 보다는 '부모클래스의 print에 virtual이 붙지않아 ,다형성으로 구현할때 오버라이딩이 안됐다. '
라고 보는게 조금 더 맞지않나 싶은데 ( 12.9 객체잘림과 reference_wrapper 강의를 보면 객체잘림을 설명하실때 객체잘림은 refernce 나 포인터가 아닌 instance 끼리의 대입연산자(=) 를 할때 이루어지는것으로 보여지거든요) 이부분은 어떻게 생각하시나요?
TaeChoon Pakr
0
2
throw; vs throw e;의 차이가 궁금합니다.
/*
예외 클래스와 상속
*/
#include <iostream>
using namespace std;
class Exception
{
public:
virtual void report() const
{
cerr << "Exception report" << endl;
}
};
class ArrayException : public Exception
{
public:
void report() const override
{
cerr << "Array exception" << endl;
}
};
class MyArray
{
private:
int m_arr[5];
public:
int& operator[] (const int& index)
{
//if (index < 0 || index > 4) throw - 1;
if (index < 0 || index > 4) throw ArrayException();
return m_arr[index];
}
};
void doSomething()
{
MyArray my_arr;
try
{
my_arr[100]; //멤버함수에서 throw를 던져도 받을 수 있음.
}
catch (const int& x)
{
cerr << "Exception" << x << endl;
}
catch (Exception & e)// Exception & e = ArrayException();
{
cout << "doSomething()" << endl;
e.report();
//rethorw
throw e; //하면 다형성이 풀림?
//throw;
}
}
int main()
{
try
{
doSomething();
}
catch (Exception & e)
{
cout << "main()" << endl;
e.report();
}
return 0;
}
throw ArrayException()하고 두번의 catch문을 모두 Exception으로 받게 해놨습니다. 그런데 처음 catch에서는 다형성 성질에 맞게 자식클래스의 report를 실행했는데 throw e로 했을 경우에는 자식클래스의 report가 아닌 부모클래스의 report를 실행합니다. 거기다가 throw;로 했을 경우에는 정상적으로 자식클래스의 report를 실행합니다.
1. throw e;로 했을 경우에는 왜 다형성이 풀리는 건가요?
2. throw;랑 throw e; 랑 차이는 무엇인가요?
dlgydlf12345
0
1