Skip to content

Commit c767b1d

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

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

lessons/l08.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
:keywords: آموزش, آموزش پایتون, آموزش برنامه نویسی, پایتون, انواع شی, انواع داده, انواع شی در پایتون, انواع داده در پایتون, پایتون
66

77

8-
درس ۰۸: انواع داده یا شی در پایتون: set ،dict ،tuple، list و None
8+
درس ۰۸: انواع داده یا شی در پایتون: set ،dict ،tuple ،list و None
99
====================================================================================
1010

1111
.. figure:: /_static/pages/08-python-built-in-data-types-2.jpg

lessons/l24.rst

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@
4040

4141
در زبان برنامه‌نویسی پایتون از دستور ``raise`` [`اسناد پایتون <https://docs.python.org/3/reference/simple_stmts.html#raise>`__] برای بروز یک Exception استفاده می‌گردد::
4242

43-
raise Exception() # an exception object
43+
raise exception_object
4444

45+
به نمونه کد زیر توجه نمایید:
4546

4647
.. code-block:: python
4748
:linenos:
@@ -93,6 +94,39 @@
9394
TypeError: The input must be 'int' type, 'C' is <class 'str'>
9495

9596

97+
طی درس پیش مشاهده کردیم، چنانچه در زمان handle کردن یک Exception، یک Exception دیگر رخ دهد؛ در نتیجه Traceback نهایی نیز شامل یک Traceback به ازای هر Exception خواهد بود. این امکان نیز توسط دستور ``raise`` برای برنامه‌نویس فراهم می‌باشد. می‌توان با استفاده از دستور ``from`` در کنار ``raise``، دو Exception - که از نظر منطقی به یکدیگر وابسته هستند - را به یکدیگر متصل و سپس raise کرد::
98+
99+
raise exception_object from other_exception_object
100+
101+
به نمونه کد ساده زیر و خروجی آن توجه نمایید:
102+
103+
.. code-block:: python
104+
:linenos:
105+
106+
def sum_int(a, b):
107+
try:
108+
return a + b
109+
except Exception as exception:
110+
raise RuntimeError("Something bad happened") from exception
111+
112+
res = sum_int(3, 'C')
113+
print(res)
114+
115+
::
116+
117+
Traceback (most recent call last):
118+
File "sample.py", line 3, in sum_int
119+
return a + b
120+
TypeError: unsupported operand type(s) for +: 'int' and 'str'
121+
122+
The above exception was the direct cause of the following exception:
123+
124+
Traceback (most recent call last):
125+
File "sample.py", line 7, in <module>
126+
res = sum_int(3, 'C')
127+
File "sample.py", line 5, in sum_int
128+
raise RuntimeError("Something bad happened") from exception
129+
RuntimeError: Something bad happened
96130

97131
ایجاد Exception
98132
~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)