Skip to content

Commit a3f1a29

Browse files
author
Saeid
committed
l24: raise is completed
1 parent c767b1d commit a3f1a29

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

lessons/l24.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,31 @@
128128
raise RuntimeError("Something bad happened") from exception
129129
RuntimeError: Something bad happened
130130

131+
به عنوان یک نمونه کاربرد، از این روش می‌توان برای ایجاد یک Wrapper برای چندین Exception بهره برد. در این حالت کد سطح بالاتر تنها نیاز است یک نوع Exception را handle نماید:
132+
133+
.. code-block:: python
134+
:linenos:
135+
136+
def sum_int(a, b):
137+
try:
138+
return a + b
139+
except TypeError as type_err:
140+
raise RuntimeError(f'Something bad happened \n => {str(type_err)}') from type_err
141+
142+
143+
144+
try:
145+
res = sum_int(3, 'C')
146+
print(res)
147+
148+
except RuntimeError as runtime_err:
149+
print(f'{runtime_err.__class__.__name__}: {str(runtime_err)}')
150+
151+
::
152+
153+
RuntimeError: Something bad happened
154+
=> unsupported operand type(s) for +: 'int' and 'str'
155+
131156
ایجاد Exception
132157
~~~~~~~~~~~~~~~~~~~~~~~~~~~
133158

0 commit comments

Comments
 (0)