Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d3edfc8

Browse filesBrowse files
author
Saeid Darvish
committed
l23: add with section
1 parent 1f12500 commit d3edfc8
Copy full SHA for d3edfc8

File tree

Expand file treeCollapse file tree

1 file changed

+47
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+47
-1
lines changed
Open diff view settings
Collapse file

‎lessons/l23.rst‎

Copy file name to clipboardExpand all lines: lessons/l23.rst
+47-1Lines changed: 47 additions & 1 deletion
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@
677677
******************************
678678
--------> Finished!
679679

680+
680681
روند انتشار Exception
681682
----------------------------
682683

@@ -718,7 +719,6 @@
718719
در نمونه کد بالا همانطور که مشخص است تمام Exceptionها در داخل تابع ``print_sum_div_first`` رخ می‌دهد ولی از آنجا که این تابع فاقد handler می‌باشد، Exceptionها به یک مرحله قبل‌تر یعنی تابع ``action`` تحویل می‌گردند، ولی این تابع تنها یک handler برای ``ZeroDivisionError`` داشته پس تمامی Exceptionهای احتمالی دیگر از جمله ``TypeError`` به یک مرحله قبل‌تر تحویل و خوشبختانه در آن‌جا handle می‌شوند!
719720

720721

721-
722722
مدیریت خطای تودرتو (Nested Exception Handling)
723723
---------------------------------------------------
724724

@@ -727,6 +727,52 @@
727727
البته از آنجا که در یکی از بندهای فلسفه پایتون آمده: `PEP 20: Flat is better than nested <https://www.python.org/dev/peps/pep-0020/>`__ انجام این‌کار چندان پایتونی نمی‌باشد و برنامه‌نویس احتمالا می‌تواند با کمی دقت بیشر از ساختار تودرتو پرهیز کند و کدی به مراتب خواناتر توسعه دهد. به هر حال امکان این کار در زبان برنامه‌نویسی پایتون برای برنامه‌نویس محفوظ نگه‌داشته شده است.
728728

729729

730+
731+
مدیریت خطا و دستور ``with``
732+
--------------------------------------
733+
734+
از درس بیست و یکم با مفهوم Context Manager و ارتباط آن با دستور ``with`` آشنا هستیم. اینکه مدیریت خطا برای این ساختار چگونه باشد به این بستگی دارد که می‌خواهیم در کدام نقطه Exception احتمالی را handle کنیم. بر اساس مفهوم Context Manager، در چند نقطه زیر احتمال بروز Exception وجود دارد:
735+
736+
* داخل متد ``__init__`` کلاس ContextManager
737+
* داخل متد ``__enter__`` کلاس ContextManager
738+
* داخل بدنه دستور ``with``
739+
* داخل متد ``__exit__`` کلاس ContextManager
740+
741+
742+
اگر برایمان مهم نباشد می‌توانیم به صورت زیر یک handler برای بروز Exceptionهای احتمالی در تمام حالات بالا پیاده‌سازی نماییم::
743+
744+
745+
try:
746+
with ContextManager():
747+
do_something()
748+
except Exception as err:
749+
pass
750+
751+
752+
در غیر این صورت می‌توانید مشابه نمونه کد زیر عمل نمایید::
753+
754+
try:
755+
context_manager = ContextManager()
756+
757+
except Exception as err:
758+
# Handler for: '__init__'
759+
760+
else:
761+
try:
762+
with context_manager:
763+
try:
764+
do_something()
765+
766+
except Exception as err:
767+
# Handler for: 'with' body
768+
769+
except Exception as err:
770+
# Handler for: '__enter__' and '__exit__'
771+
772+
773+
[`PEP 343 - Specification: The 'with' Statement <https://www.python.org/dev/peps/pep-0343/#specification-the-with-statement>`__]
774+
775+
730776
کارایی (Performance)
731777
----------------------------
732778

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.