Lecture 7
Exception Handling
Introduction to Engineering Design
JeHeon Ryoo
Department of Integrated Display Engineering
Reference from: “혼자 공부하는 파이썬 개정판” published by 한빛미디어
2024-05-08
2024 1학기 공학기초설계
1
Lecture 7
Table of Contents
1. Syntax Error & Exception
0. Quick Review of Lec.6
• 1-1. f(x) : Def, Body, Return
• 1-1. Types of Error
• 1-2. Local Variable & “global” Keyword
• 1-2. Basic Exception Handling
• 1-3. Lambda
• 1-3. “try except” Syntax
• 1-4. “finally” Syntax: Exercise
2. Exception Advanced
3. HW#4
• 2-1. Exception Object
• 3-1. Exception Handling for List [ ]
• 2-2. Exception Classification
• 3-2. Syntax Error(구문 오류) vs. Exception(예외)
• 2-3. Catching All Exceptions
• 3-3. Extra Notice!
• 2-4. “raise” Syntax
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
2
0. Quick Review of Lec.6
2024-05-08
3
Lecture 7
0-1. f(x) : Def, Body, Return(1)
• Functions are used to abstract
components of a program.
• They take some input and then
process it to find the result.
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
4
Lecture 7
0-1. f(x) : Def, Body, Return(2)
• Start a function definition with the keyword def.
• Then comes the function name, with arguments in braces, and then a colon.
• Next comes, indented, the body of the function
• Finally, use return to specify the output.
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
5
Lecture 7
0-1. f(x) : Def, Body, Return(3)
• By default, Python returns None.
• Once Python hits return, it will return the output and jump out of the
function.
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
6
Lecture 7
0-2. Local Variable & “global” Keyword
• Variables defined within a function (local),
are only accessible within the function.
2024-05-08
2024 1학기 공학기초설계
• We could (but should not) change
global variables within a function.
AOSL
Ryoo, JeHeon
7
Lecture 7
0-3. Lambda
• An alternative way to define short functions.
• Pros :
➢ One line / in line.
➢ No need to name a function.
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
8
1. Syntax Error & Exception
2024-05-08
9
Lecture 7
1-1. Types of Error
Syntax Error(구문 오류):
프로그램 실행 전에 발생하는 오류
vs
Exception(예외):
프로그램 실행 중에 발생하는 오류
A.K.A. Runtime Error(런타임 오류)
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
10
Lecture 7
1-1. Types of Error: Syntax Error(구문 오류)
EOL : End of Line
“ 추가
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
11
Lecture 7
1-1. Types of Error: Exception(예외)
일단 프로그램이 실행되긴 했음
→ “프로그램 시작” 출력
But, list_a[1]를 읽을 때 NameError 발생!
2024-05-08
2024 1학기 공학기초설계
list_a를 정의하면 오류 해결!
But, 프로그램을 만들다 보면 더
복잡한 방법으로 처리해야 됨!
AOSL
Ryoo, JeHeon
12
Lecture 7
1-2. Basic Exception Handling
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
13
Lecture 7
1-2. Basic Exception Handling: Using Conditional Statement
isdigit() : 숫자로만 구성된 글자인지 확인하는 문자열 함수
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
14
Lecture 7
1-3. “try except” Syntax
try:
예외가 발생할 가능성이 있는 코드
except:
예외가 발생했을 때 실행할 코드
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
15
Lecture 7
1-3. “try except” Syntax: Using with “pass” keyword
try:
예외가 발생 가능 코드
except:
pass
float() : 숫자나 문자열을 실수형으로 변환
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
16
Lecture 7
1-3. “try except” Syntax: “try except else”
try:
예외가 발생할 가능성이 있는 코드
except:
예외가 발생했을 때 실행할 코드
else:
예외가 발생하지 않았을 때 실행할 코드
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
17
Lecture 7
1-4. “finally” Syntax: Exercise
try:
예외가 발생할 가능성이 있는 코드
except:
예외가 발생했을 때 실행할 코드
else:
예외가 발생하지 않았을 때 실행할 코드
finally:
무조건 실행할 코드
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
18
2. Exception Advanced
2024-05-08
19
Lecture 7
2-1. Exception Object(예외 객체)
try:
예외가 발생할 가능성이 있는 구문
except 예외의 종류 as 예외 객체를 활용할 변수 이름:
예외가 발생했을 때 실행할 코드
When first using exception object, you can be confused
when you don’t know the “type of exception”.
Then we use mother of all exception → Exception
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
20
Lecture 7
2-1. Exception Object: Example
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
21
Lecture 7
2-2. Exception Classification: Case for several exceptions
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
22
Lecture 7
2-2. Exception Classification: Exception Type
try:
예외가 발생할 가능성이 있는 구문
except 예외 종류 A:
예외A가 발생했을 때 실행할 구문
except 예외 종류 B:
예외B가 발생했을 때 실행할 구문
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
23
Lecture 7
2-2. Exception Classification: Exception Syntax & Object
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
24
Lecture 7
2-3. Catching All Exceptions: NameError
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
25
Lecture 7
2-3. Catching All Exceptions: Using “Exception” Class
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
26
Lecture 7
2-3. “raise” Syntax
raise 예외 객체
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
27
3. HW#4
2024-05-08
28
Lecture 7
3-1. Exception Handling for List [ ] (1)
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
29
Lecture 7
3-1. Exception Handling for List [ ] (2)
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
30
Lecture 7
3-2. Syntax Error(구문 오류) vs. Exception(예외)
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
31
Lecture 7
3-3. Extra Notice!
• 안남영 교수님 로스 알라모스 국립연구소 멘토
• 5월 15일 석가탄신일 보강으로 세미나 참가!
• 중요: 출석 점수 반영합니다!!!!
• 금요일 수업 없으면 6월 7일 신촌 오후 2시 필참
2024-05-08
2024 1학기 공학기초설계
AOSL
Ryoo, JeHeon
32