0단계 - 설명 좀~


- 파이썬에서 예외처리를 하고 싶다.
- 에러가 났을 때 처리를 잘해야 제품과 서비스가 되지 않겠는가?
- ㅎㅎㅎ
1단계 - 바로 try except 훈련
try:
print(x)
except:
print("에러 발생")
> 결과
에러 발생
>
https://www.w3schools.com/python/trypython.asp?filename=demo_try_except <-- 온라인에서 바로 확인 가능
2단계 - 예외 종류 별 받기 훈련
BaseException
+-- SystemExit
+-- KeyboardInterrupt
+-- GeneratorExit
+-- Exception
+-- StopIteration
+-- StopAsyncIteration
+-- ArithmeticError
| +-- FloatingPointError
| +-- OverflowError
| +-- ZeroDivisionError
+-- AssertionError
+-- AttributeError
+-- BufferError
+-- EOFError
+-- ImportError
| +-- ModuleNotFoundError
+-- LookupError
| +-- IndexError
| +-- KeyError
+-- MemoryError
+-- NameError
| +-- UnboundLocalError
+-- OSError
| +-- BlockingIOError
| +-- ChildProcessError
| +-- ConnectionError
| | +-- BrokenPipeError
| | +-- ConnectionAbortedError
| | +-- ConnectionRefusedError
| | +-- ConnectionResetError
| +-- FileExistsError
| +-- FileNotFoundError
| +-- InterruptedError
| +-- IsADirectoryError
| +-- NotADirectoryError
| +-- PermissionError
| +-- ProcessLookupError
| +-- TimeoutError
+-- ReferenceError
+-- RuntimeError
| +-- NotImplementedError
| +-- RecursionError
+-- SyntaxError
| +-- IndentationError
| +-- TabError
+-- SystemError
+-- TypeError
+-- ValueError
| +-- UnicodeError
| +-- UnicodeDecodeError
| +-- UnicodeEncodeError
| +-- UnicodeTranslateError
+-- Warning
+-- DeprecationWarning
+-- PendingDeprecationWarning
+-- RuntimeWarning
+-- SyntaxWarning
+-- UserWarning
+-- FutureWarning
+-- ImportWarning
+-- UnicodeWarning
+-- BytesWarning
+-- EncodingWarning
+-- ResourceWarning
- 파이썬이 정의한 기본 예외들이다.
- 이 중에 "NameError"로 훈련하겠습니다. 아자~!
- Name에 뭔가 문제가 있다는 것입니다.
- 변수를 정의하지 않고 바로 사용하도록 하겠습니다.
- 위 예외에 대해서 받아서 처리 할 수 있다는!
- https://docs.python.org/3/library/exceptions.html
Built-in Exceptions — Python 3.10.3 documentation
In Python, all exceptions must be instances of a class that derives from BaseException. In a try statement with an except clause that mentions a particular class, that clause also handles any exception classes derived from that class (but not exception cla
docs.python.org
try:
print(x)
except NameError:
print("이름이 이상해")
except:
print("뭔가 문제가 있구먼!!!")
> 실행
이름이 이상해
>
3단계 - raise Exception("") 훈련하기.
- 강제로 예외를 발생시키고 싶을 때 "raise Exception()"을 사용한다.
try:
n = 10
if n == 10:
raise Exception("예외")
except:
print("강제 예외 발생")
참조
'linux' 카테고리의 다른 글
| 특정 프로세스를 찾아서 종료시키기 (0) | 2022.10.17 |
|---|---|
| iptables 기초 (0) | 2022.09.27 |
| ubuntu 설치 by USB (0) | 2022.03.23 |
