try-except 문에서 except를 as로 받지 않고 그냥 raise하면 try문에서 발생했던 에러를 그대로 일으킨다.하위 두 코드는 같다.
In [1]: try:
...: Notification.objects.get(id=3434)
...: except Notification.DoesNotExist as e:
...: raise e
...:
DoesNotExist: Notification matching query does not exist.
In [1]: try:
...: Notification.objects.get(id=3434)
...: except Notification.DoesNotExist:
...: raise
...:
DoesNotExist: Notification matching query does not exist.
>>> try:
... 1 / 0
... except ZeroDivisionError:
... raise
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
>>> try:
... 1 / 0
... except ZeroDivisionError as e:
... raise ValueError(e)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ValueError: division by zero
>>> try:
... 1 / 0
... except ZeroDivisionError as e:
... raise ValueError(e) from e
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ValueError: division by zero
>>> try:
... 1 / 0
... except ZeroDivisionError as e:
... raise ValueError('ERROR!')
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ValueError: ERROR!
>>> try:
... 1 / 0
... except ZeroDivisionError as e:
... raise ValueError('ERROR!') from e
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
ValueError: ERROR!