U E D R , A S I H C RSS

미시Cpp/0511

다시 출발합니다

1. 미시Cpp

회차 : 5회차
시간 : 11시 30분 ~
장소 : 6층

1.1. 참가원

멘토 장용운 출석
멘티 유재범 출석
신형철 출석

1.2. 이번에 배울 것

템플릿 기초
  • 템플릿의 기본
  • 함수 템플릿과 템플릿 함수
  • 템플릿 클래스
  • 데이터 타입이 아닌 템플릿 파라미터

예외 처리 기초
  • 예외 처리 문법
    ○ try~catch
    ○ throw
  • stack unwinding
  • throw 리스트
  • 함수 또는 메서드에서의 nothrow
  • unexpected_handler, terminate_handler
  • std::exception

실습
  • 체스 프로그램 틀 잡기

2. 스터디 진행

2.1. 내용

  • unexpected_handler
    • throw 리스트에 명시되지 않은 throw가 발생했을 때 발생한다.
  • terminate_handler
    • 예외 처리가 도저히 불가능할 때 발생하는 것으로, 일단 발생하고 나면 회생 절차가 없다.
  • 함수 템플릿
    • 템플릿을 기초로 하여 구현한 함수.
  • 템플릿 함수
    • 템플릿을 이용하여 실체화한 함수.

2.2. 코드

  • 템플릿 클래스/ 함수 템플릿

template <typename T>
class Test
{
public:
	T add(T a, T b) { return a + b; }

	template <typename E>
	E sub(E a, E b) { return a - b; }
};
  • 템플릿 파라미터

template <int length>
class Test
{
private:
	int * arr;

public:
	Test() { arr = new int[length]; }
};

void main(void)
{
	Test<4> t;
}
  • Try/Catch/Throw

#include <iostream>
#include <exception>

class Test
{
public:
	int invokeException() throw (std::exception)
	{
		throw std::exception();
	}
};

int main(void)
{
	Test t;

	try
	{
		t.invokeException();
	}
	catch (std::exception e)
	{
		std::cout << "Catch!" << std::endl;
	}

	return 0;
}
  • Stack unwinding

class CustomException
{
public:
	void testMethod() throw(std::exception);
};

class CustomExceptionChild : public CustomException
{
public:
	void testMethod() throw(std::exception);
};

class CustomeExceptionChildChild : public CustomExceptionChild
{
public:
	void testMethod() throw(std::exception);
};
 // 자식 클래스에서 예외가 발생했음에도 불구하고 catch 구문이 없을 때, 작업을 중단하고 catch 구문을 발견할 때까지 부모 클래스로 예외 처리가 전달되는 현상.
  • throw 리스트와 nothrow

class CustomException
{
public:
	static void Test() _NOEXCEPT; // 예외가 발생하지 않음을 보장합니다.
	void ExceptTest() throw ();   // throw 리스트 내부에 있는 예외가 발생할 수 있습니다.
};
 // _NOEXCEPT를 선언했는데 예외가 발생할 경우... 재밌는 일이 일어납니다.
* std::exception
class MyException : public std::exception
{
public:
	const char * what() const _NOEXCEPT
	{
		  return "Oooops!\n";
	}
};

// std::exception에 들어있는 what 메서드가 중심.

3. 잡담


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:32
Processing time 0.0243 sec