Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit daaa45b

Browse files
committed
Exceptions
1 parent 849ee09 commit daaa45b

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,7 @@ except (<exception>, [...]) as <name>:
13741374
```
13751375
* **Also catches subclasses of the exception.**
13761376
* **Use `'traceback.print_exc()'` to print the error message to stderr.**
1377+
* **Use `'print(<name>, file=sys.stderr)'` to print just the cause of the exception.**
13771378

13781379
### Raising Exceptions
13791380
```python
@@ -1396,7 +1397,7 @@ exc_type = <name>.__class__
13961397
filename = <name>.__traceback__.tb_frame.f_code.co_filename
13971398
func_name = <name>.__traceback__.tb_frame.f_code.co_name
13981399
line = linecache.getline(filename, <name>.__traceback__.tb_lineno)
1399-
error_msg = traceback.format_exception(exc_type, <name>, <name>.__traceback__)
1400+
error_msg = ''.join(traceback.format_exception(exc_type, <name>, <name>.__traceback__))
14001401
```
14011402

14021403
### Built-in Exceptions

index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,7 @@
13111311
<ul>
13121312
<li><strong>Also catches subclasses of the exception.</strong></li>
13131313
<li><strong>Use <code class="python hljs"><span class="hljs-string">'traceback.print_exc()'</span></code> to print the error message to stderr.</strong></li>
1314+
<li><strong>Use <code class="python hljs"><span class="hljs-string">'print(&lt;name&gt;, file=sys.stderr)'</span></code> to print just the cause of the exception.</strong></li>
13141315
</ul>
13151316
<div><h3 id="raisingexceptions">Raising Exceptions</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">raise</span> &lt;exception&gt;
13161317
<span class="hljs-keyword">raise</span> &lt;exception&gt;()
@@ -1327,7 +1328,7 @@
13271328
filename = &lt;name&gt;.__traceback__.tb_frame.f_code.co_filename
13281329
func_name = &lt;name&gt;.__traceback__.tb_frame.f_code.co_name
13291330
line = linecache.getline(filename, &lt;name&gt;.__traceback__.tb_lineno)
1330-
error_msg = traceback.format_exception(exc_type, &lt;name&gt;, &lt;name&gt;.__traceback__)
1331+
error_msg = <span class="hljs-string">''</span>.join(traceback.format_exception(exc_type, &lt;name&gt;, &lt;name&gt;.__traceback__))
13311332
</code></pre></div>
13321333

13331334
<div><h3 id="builtinexceptions">Built-in Exceptions</h3><pre><code class="text language-text">BaseException

0 commit comments

Comments
 (0)