11

(exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우
Page 2: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

예외(exception)

프로그램 실행 중에 잘못된 코드, 잘못된 데이터, 하드웨어 장애 등 예외적인 상황에서 발생하는 오류(error)

예)- 나눗셈의 분모가 0가 되는 경우- 배열의 첨자 범위를 벗어나는 경우- 입력파일의 부재

예외처리(exception handling)

일반적으로 예외가 발생하면 프로그램이 강제 종료되거나잘못된 결과가 출력됨

예외처리란 예외 발생을 탐지하여 에러를 처리하고 실행을 계속하거나 프로그램을 정상적으로 종료시킴

Page 3: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

전통적인 예외처리

프로그램 실행 중에 발생할 수 있는 예외적인 상황을 예측하여, 조건문을 사용하여 예외가 발생하는지를 검사하여 처리함

처리 예)- 예외 상황에 대한 메시지를 출력하고 프로그램 종료- 예외 상황에 따라 적절한 처리 후 프로그램 실행

※ 버거와 예외

프로그램의 로직 에러와 같은 버거는 프로그램 작성과정에서 수정되어야 함

Page 4: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

// 예외 발생 프로그램#include <iostream>using namespace std;

int main(){

int tasuk, anta;double tayul;

cout << "* 타석수와 안타수는 ";cin >> tasuk >> anta;

tayul = (double) anta/ (double) tasuk;

cout << tayul << endl;return 0;

}

※ 전통적인 예외 처리

타석수가 0가 입력되는 경우

1. “타석수가 0이므로 타율 계산할 수 없음” 이란 메시지를내고 종료함

2. “타석수가 0입니다. 다시 입력해 주세요” 란 메시지를 내고계속 실행

Page 5: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

try-throw-catch

- try { } 블록 : 예외가 발생할 가능성이 있는 코드를 묶음- throw 문 : 예외 발생을 탐지함, try 블록내에 존재- catch 문 : throw 문에 의해 탐지된 예외를 처리함

형식)try{

if(예외 조건)throw 예외값; //변수, 상수, 객체

:}catch(예외 매개 변수){

예외 처리 문장들}

Page 6: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

// 예외처리 프로그램(1)#include <iostream>using namespace std;

int main(){

int tasuk, anta;double tayul;

try{cout << "* 타석수와 안타수는";cin >> tasuk >> anta;

if(tasuk == 0)throw tasuk;

tayul = (double) anta/ (double) tasuk;

cout << tayul << endl;}catch(int t){

cout << " -> 타석수가 0\n”;}

return 0;}

Page 7: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

// 예외처리 프로그램(2)#include <iostream>using namespace std;

int main(){

int tasuk, anta;double tayul;

try{cout << "* 타석수와 안타수는";cin >> tasuk >> anta;

if(tasuk == 0)throw tasuk;

if(tasuk < anta)throw tasuk;

tayul = (double) anta/ (double) tasuk;

cout << tayul << endl;}catch(int t){if(t==0)cout << " -> 타석수가 0\n”;

elsecout << " -> 데이터오류\n”;

}

return 0;}

Page 8: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

예외 처리 과정

try 블록을 실행하는 과정에서- throw 문장을 만나면 catch 문으로 분기하여 실행하고

catch 문 다음 문장을 실행- throw 문장을 만나지 않고 try 블록 끝에 도달하면

catch 문을 실행하지 않고 catch 문 다음 문장을 실행

형식)try{

if(예외 조건)throw 예외값; //변수, 상수, 객체

:}catch(예외 매개 변수){

예외 처리 문장들}

Page 9: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

다중 catch 문

하나의 try 블록에서 throw 문장이 여러 개일 때 throw 예외값의 자료형에 따라 여러 개의 catch 문이 존재할 수 있고, 이때 자료형에 맞는 catch 문이 실행됨

catch(…) 문

throw 예외값의 자료형에 관계없이 모든 경우의 예외처리

※ 다중 catch 문 실행 순서

throw 예외값이 여러 개의 catch 문에 해당되는 경우는 먼저 만나는 catch 문이 실행되므로, 구체적인 예외를 일반적인 예외보다 앞에 두어야함

자료형이 맞는 catch 문이 없는 경우는 abort()함수 실행

Page 10: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

// 예외처리 프로그램(3)#include <iostream>using namespace std;

int main(){

int tasuk, anta;double tayul;

try{cout << "* 타석수와 안타수는";cin >> tasuk >> anta;

if(tasuk == 0)throw “zero”;

if(tasuk < anta)throw tasuk;

tayul = (double) anta/ (double) tasuk;

cout << tayul << endl;}catch(char *e){

cout << " -> 데이터오류\n”;}catch(int t){

cout << " -> 타석수가 0\n”;}

return 0;}

Page 11: (exception) - contents.kocw.netcontents.kocw.net/KOCW/document/2014/hanbat/ahnkeehong/11.pdf · 어장애등예외적인상황에서발생하는오류(error) 예) - 나눗셈의분모가0가되는경우

// 예외처리 프로그램(4)#include <iostream>using namespace std;

int main(){

int tasuk, anta;double tayul;

try{cout << "* 타석수와 안타수는";cin >> tasuk >> anta;

if(tasuk == 0)throw “zero”;

if(tasuk < anta)throw tasuk;

tayul = (double) anta/ (double) tasuk;

cout << tayul << endl;}catch(…){

cout << " -> 데이터오류\n”;}

return 0;}