内容简介
主要分为两个部分
第一部分:将CSDN博客的html源码进行复制并转发到自己的blog上(大家记得发文章的时候要选择转载哦)
第二部分:将CSDN博客的页面转换成Markdown代码形式,这样可以方便自己的修改和保存,也方便大家部分的截取别人博客中的精华内容
Html方式转载CSDN博客
以如下这篇bolg做例子
https://blog.csdn.net/qq_41554005/article/details/97107087
打开要转载的博客,然后鼠标右键就会出现下面的菜单
点击审查或者有的是检查 反正就是查看他的html源代码,选中“article_content”从图片左边我们也可以看到,博客的内容已经被我们选中了,也就是我们想要转载的内容
选中之后右键复制保存为HTML
之后走正常发布自己的bolg流程就可以了
调整到markdown文本编辑器之后把之前复制的内容粘贴进去就可以了
如下图所示
保存CSDN博客的Markdown文件
如果需要对bolg中的内容进行修改或者部分截取或者只想自己写好了保存成比较符合自己审美的md文件的话可以采用如下方式
还是找一个blog为例子,复制完成HTML文件之后进行如下操作
备注:这种方法在拷贝html文件的时候对表格和代码段的转换可能会出一些问题 代码段可能会出现一个代码行号 大量不规则的表格的时候可能会出现一个对不齐的问题,但是相对于文字和图片等等基本是没有问题的(如果还有其他的问题请留言反馈我去看看有没有更好的转换方式,谢谢大家啦)
安装html2text
比较详细的使用技巧
win或者linux中均可使用
html2text是一个Python模块,用来把HTML格式转换为文本(Markdown)格式。
$ pip install html2text
import html2text
html = '''
(你复制的html代码)
'''
markdown = html2text.html2text(html)
print(type(markdown))
print(markdown)
f2 = open('blog.md','w')
f2.write(markdown)
f2.close()
eg.复制之后运行就可以输出一个md文件啦
打开这个md文件就是这样的 同学们可以修改或者自己进行增补之后在发布
import html2text
html = '''
<div id="article_content" class="article_content clearfix">
<div class="article-copyright">
<span class="creativecommons">
<a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">
</a>
<span>版权声明:本文为博主原创文章,遵循<a href="http://creativecommons.org/licenses/by-sa/4.0/" target="_blank"> CC 4.0 by-sa </a>版权协议,转载请附上原文出处链接和本声明。 </span>
<div class="article-source-link2222">
本文链接:<a href="https://blog.csdn.net/qq_41554005/article/details/92246002">https://blog.csdn.net/qq_41554005/article/details/92246002</a>
</div>
</span>
</div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
<div id="content_views" class="markdown_views prism-atom-one-dark">
<!-- flowchart 箭头图标 勿删 -->
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
</svg>
<h1><a name="t0"></a><a id="python3_try__raise__0"></a>python3 中try 异常调试 raise 异常抛出</h1>
<p></p><div class="toc"><h3><a name="t1"></a>文章目录</h3><ul><li><a href="#python3_try__raise__0" rel="nofollow" data-token="8f4fce4701bedc629ba95f27556595b6" target="_self">python3 中try 异常调试 raise 异常抛出</a></li><ul><li><a href="#_3" rel="nofollow" data-token="88b6bcbf24e6fba5feef19ee5525f23e" target="_self">一、什么是异常?</a></li><li><a href="#_29" rel="nofollow" data-token="eb490ac434dfa9d7c526c12801124970" target="_self">二、基础异常处理</a></li><ul><li><a href="#_47" rel="nofollow" data-token="b4f09150eb9f65ecd945ea2365509a0a" target="_self">基础语法</a></li></ul><li><a href="#_96" rel="nofollow" data-token="bf4140de05e3ddc411942820e21630da" target="_self">三、捕获异常的操作</a></li><ul><li><a href="#31_except_100" rel="nofollow" data-token="27126025ceec6960e2aa4d93ec086f1d" target="_self">3.1 使用except而不带任何异常类型</a></li><li><a href="#32except_119" rel="nofollow" data-token="08a00fd6653b3d36616b71633f0fe03b" target="_self">3.2使用except而带多种异常类型</a></li><li><a href="#33tryexcept_180" rel="nofollow" data-token="3d3fc537e0ae8ee1e229256a2d074bcf" target="_self">3.3使用多层try的时候except的传递</a></li></ul><li><a href="#_234" rel="nofollow" data-token="7b4f3cef12ebe33b9131d8be405da99c" target="_self">四、自己抛出异常</a></li><li><a href="#_287" rel="nofollow" data-token="427ee441606bc39abb647edae1a0c3f8" target="_self">五、异常信息的详细处理打印</a></li><li><a href="#_323" rel="nofollow" data-token="1bd7a898b7f572c213d213834f7c5e3e" target="_self">六、创建自己的异常</a></li><li><a href="#_428" rel="nofollow" data-token="3080492ac1a8b50ac4788171c66dde37" target="_self">七、常用模块</a></li></ul></ul></div><p></p>
<h2><a name="t2"></a><a id="_3"></a>一、什么是异常?</h2>
<p>异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行。</p>
<p>一般情况下,在Python无法正常处理程序时就会发生一个异常。</p>
<p>异常是Python对象,表示一个错误。</p>
<p>当Python脚本发生异常时我们需要捕获处理它,否则程序会终止执行。<br>
常见异常</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment"># AttributeError 调用不存在的方法引发的异常</span>
<span class="token comment"># EOFError 遇到文件末尾引发的异常</span>
<span class="token comment"># ImportError 导入模块出错引发的异常</span>
<span class="token comment"># IndexError 列表月越界引发的异常</span>
<span class="token comment"># IOError I/O操作引发的异常,如打开文件出错等</span>
<span class="token comment"># KeyError 使用字典中不存在的关键字引发的异常</span>
<span class="token comment"># NameError 使用不存在的变量名引发的异常</span>
<span class="token comment"># TabError 语句块缩进不正确引发的异常</span>
<span class="token comment"># ValueError 搜索列表中不存在值引发的异常</span>
<span class="token comment"># ZeroDivisionError 除数为零引发的异常</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre>
<h2><a name="t3"></a><a id="_29"></a>二、基础异常处理</h2>
<p>捕捉异常可以使用try/except语句,见下例子。</p>
<p>try/except语句用来检测try语句块中的错误,从而让except语句捕获异常信息并处理。</p>
<p>如果你不想在异常发生时结束你的程序,只需在try里捕获它。</p>
<p>try的工作原理是,当开始一个try语句后,python就在当前程序的上下文中作标记,这样当异常出现时就可以回到这里,try子句(与try同级的except等)先执行,接下来会发生什么依赖于执行时是否出现异常。</p>
<p>如果当try后的语句执行时发生异常,python就跳回到try并执行第一个匹配该异常的except子句,异常处理完毕,控制流就通过整个try语句(除非在处理异常时又引发新的异常)。</p>
<p>如果在try后的语句里发生了异常,却没有匹配的except子句,异常将被递交到上层的try,或者到程序的最上层(这样将结束程序,并打印缺省的出错信息)。</p>
<p>如果在try子句执行时没有发生异常,python将执行else语句后的语句(如果有else的话),然后控制流通过整个try语句。</p>
<p>不管在try有无异常抛出时都会执行本级try对应的finally。</p>
<h3><a name="t4"></a><a id="_47"></a>基础语法</h3>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">try</span><span class="token punctuation">:</span>
检测异常代码段
<span class="token keyword">except</span><span class="token punctuation">:</span>
发生异常后执行代码段
<span class="token keyword">else</span><span class="token punctuation">:</span>
未发生异常执行代码段
<span class="token keyword">finally</span><span class="token punctuation">:</span>
最终执行代码段
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>
<p>例子:</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"test1"</span><span class="token punctuation">)</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
s <span class="token operator">=</span> <span class="token builtin">input</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> s <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"s 是空对象"</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#这句抛出异常 </span>
<span class="token keyword">except</span> TypeError<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"类型错误空对象没有长度"</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"no problem"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'end test1'</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/2019071320402593.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"test1"</span><span class="token punctuation">)</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
s <span class="token operator">=</span> <span class="token boolean">None</span>
<span class="token keyword">if</span> s <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"s 是空对象"</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#这句抛出异常 </span>
<span class="token keyword">except</span> TypeError<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"类型错误空对象没有长度"</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"no problem"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'end test1'</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204047857.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<h2><a name="t5"></a><a id="_96"></a>三、捕获异常的操作</h2>
<p>为了能够捕获异常,"except"语句必须有用相同的异常来抛出类对象或者字符串。</p>
<h3><a name="t6"></a><a id="31_except_100"></a>3.1 使用except而不带任何异常类型</h3>
<p>你可以不带任何异常类型使用except,如下实例以上方式try-except语句捕获所有发生的异常。但这不是一个很好的方式,我们不能通过该程序识别出具体的异常信息。因为它捕获所有的异常。</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"test2"</span><span class="token punctuation">)</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
x <span class="token operator">=</span> <span class="token number">1</span>
y <span class="token operator">=</span> <span class="token number">0</span>
z<span class="token operator">=</span> x<span class="token operator">/</span>y
<span class="token keyword">except</span><span class="token punctuation">:</span><span class="token comment">#捕获所有异常</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'there is problem'</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'no problem'</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'end test2'</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li></ul></pre>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL1pob3V6aXF1blpaUVpaUS9waWNCZWQvbWFzdGVyLzIwMTkwNzAxMjIzNjM5LnBuZw" alt=""></p>
<h3><a name="t7"></a><a id="32except_119"></a>3.2使用except而带多种异常类型</h3>
<p>你也可以使用相同的except语句来处理多个异常信息,这些异常将被放在一个括号里成为一个元组,如下所示:</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">try</span><span class="token punctuation">:</span>
正常的操作
<span class="token keyword">except</span><span class="token punctuation">(</span>Exception1<span class="token punctuation">[</span><span class="token punctuation">,</span> Exception2<span class="token punctuation">[</span><span class="token punctuation">,</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>ExceptionN<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
发生以上多个异常中的一个,执行这块代码
<span class="token keyword">else</span><span class="token punctuation">:</span>
如果没有异常执行这块代码
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li></ul></pre>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'test3'</span><span class="token punctuation">)</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
x <span class="token operator">=</span> <span class="token number">1</span>
y <span class="token operator">=</span> <span class="token number">0</span>
z<span class="token operator">=</span> x<span class="token operator">/</span>y
<span class="token keyword">except</span> <span class="token punctuation">(</span>NameError<span class="token punctuation">,</span>ZeroDivisionError<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"problem is (NameError,ZeroDivisionError)"</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> <span class="token punctuation">(</span>RuntimeError<span class="token punctuation">,</span> TypeError<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"problem is (RuntimeError, TypeError)"</span><span class="token punctuation">)</span>
<span class="token keyword">except</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"problem"</span><span class="token punctuation">)</span>
<span class="token keyword">raise</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"no problem"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'end test3'</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204106214.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="[外链图片转存失败(img-wLBKj7yo-1563021454247)(https://raw.githubusercontent.com/ZhouziqunZZQZZQ/picBed/master/20190701223657.png)]"></p>
<p>最后一个except子句可以忽略异常的名称,它将被当作通配符使用。你可以使用这种方法打印一个错误信息,然后再次把异常抛出。</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">import</span> sys
<span class="token keyword">try</span><span class="token punctuation">:</span>
f <span class="token operator">=</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'myfile.txt'</span><span class="token punctuation">)</span>
s <span class="token operator">=</span> f<span class="token punctuation">.</span>readline<span class="token punctuation">(</span><span class="token punctuation">)</span>
i <span class="token operator">=</span> <span class="token builtin">int</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span>strip<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># except OSError as err:</span>
<span class="token comment"># print("OS error: {0}".format(err))</span>
<span class="token keyword">except</span> ValueError<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Could not convert data to an integer."</span><span class="token punctuation">)</span>
<span class="token keyword">except</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Unexpected error:"</span><span class="token punctuation">,</span> sys<span class="token punctuation">.</span>exc_info<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> sys<span class="token punctuation">.</span>exc_info<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token string">'line xxx'</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"end"</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204129849.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="[外链图片转存失败(img-iEKHAXeS-1563021454248)(https://raw.githubusercontent.com/ZhouziqunZZQZZQ/picBed/master/20190701223723.png)]"><br>
注意有多个expect的时候会首先执行第一个能被捕获到的异常并且只执行一个</p>
<h3><a name="t8"></a><a id="33tryexcept_180"></a>3.3使用多层try的时候except的传递</h3>
<p>多重异常的处理</p>
<p>可以在try语句中嵌套另一个try语句</p>
<p>一旦发生异常,python匹配最近的except语句,</p>
<p>若是内部except能够处理该异常,则外围try语句不会捕获异常。</p>
<p>若是不能,或者忽略,外围try处理</p>
<p>内层异常捕获失败执行内层finally跳出外层执行异常捕获</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">try</span><span class="token punctuation">:</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
x <span class="token operator">=</span> <span class="token number">1</span>
y <span class="token operator">=</span> <span class="token number">0</span>
z<span class="token operator">=</span> x<span class="token operator">/</span>y
<span class="token keyword">except</span> NameError<span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"NameError"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"Finally inside"</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> <span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"All exception outside"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"Finally outside"</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204320531.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">try</span><span class="token punctuation">:</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
x <span class="token operator">=</span> <span class="token number">1</span>
y <span class="token operator">=</span> <span class="token number">0</span>
z<span class="token operator">=</span> x<span class="token operator">/</span>y
<span class="token keyword">except</span> ZeroDivisionError<span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"ZeroDivisionError"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"Finally inside"</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> <span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"All exception outside"</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"No exception outside"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"Finally outside"</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre>
<p>内层捕获成功执行expect finally 执行外层else finally</p>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL1pob3V6aXF1blpaUVpaUS9waWNCZWQvbWFzdGVyLzIwMTkwNzAxMjIzNzQ4LnBuZw" alt=""></p>
<h2><a name="t9"></a><a id="_234"></a>四、自己抛出异常</h2>
<p>触发异常时候,我们可以使用raise语句自己触发异常。raise 唯一的一个参数指定了要被抛出的异常。它必须是一个异常的实例或者是异常的类(也就是 Exception 的子类)。</p>
<p>如果你只想知道这是否抛出了一个异常,并不想去处理它,那么一个简单的 raise 语句就可以再次把它抛出。</p>
<p>raise语法格式如下:</p>
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">raise [Exception [, args [, traceback]]]
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li></ul></pre>
<p>语句中 Exception 是异常的类型,参数标准异常中任一种,args 是自已提供的异常参数。最后一个参数是可选的(在实践中很少使用),如果存在,是跟踪异常对象。</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"test4"</span><span class="token punctuation">)</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
s <span class="token operator">=</span> <span class="token boolean">None</span>
<span class="token keyword">if</span> s <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span><span class="token string">"s 是空对象"</span><span class="token punctuation">)</span>
<span class="token keyword">raise</span> NameError <span class="token comment">#如果引发NameError异常,后面的代码将不能执行</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#这句不会执行,但是后面的except还是会走到</span>
<span class="token keyword">except</span> TypeError<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"类型错误空对象没有长度"</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> NameError<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"接收到raise的异常NameError"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'end test4'</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204301275.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>抛出异常时候的参数附加显示</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"test5"</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">mye</span><span class="token punctuation">(</span> level <span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> level <span class="token operator"><</span> <span class="token number">1</span><span class="token punctuation">:</span>
<span class="token keyword">raise</span> Exception<span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">(</span>level<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">" is Invalid level!"</span><span class="token punctuation">)</span>
<span class="token comment"># 触发异常后,后面的代码就不会再执行</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
mye<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment"># 触发异常</span>
<span class="token keyword">except</span> Exception <span class="token keyword">as</span> err<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>Exception<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"successfully"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'end test5'</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204240693.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="[外链图片转存失败(img-PDoy6KNg-1563021454252)(https://raw.githubusercontent.com/ZhouziqunZZQZZQ/picBed/master/20190701223856.png)]"></p>
<h2><a name="t10"></a><a id="_287"></a>五、异常信息的详细处理打印</h2>
<p>使用sys模块可以将异常详细信息打印出来</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">import</span> sys
<span class="token keyword">try</span><span class="token punctuation">:</span>
x <span class="token operator">=</span> <span class="token number">1</span>
y <span class="token operator">=</span> <span class="token number">0</span>
z<span class="token operator">=</span> x<span class="token operator">/</span>y
<span class="token keyword">except</span> <span class="token punctuation">:</span>
t<span class="token punctuation">,</span> v<span class="token punctuation">,</span> tb <span class="token operator">=</span> sys<span class="token punctuation">.</span>exc_info<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>v<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>tb<span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204223847.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNTU0MDA1,size_16,color_FFFFFF,t_70" alt="在这里插入图片描述"></p>
<p>捕获异常后间接答应</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">def</span> <span class="token function">temp_convert</span><span class="token punctuation">(</span>var<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token builtin">int</span><span class="token punctuation">(</span>var<span class="token punctuation">)</span>
<span class="token keyword">except</span> ValueError <span class="token keyword">as</span> e<span class="token punctuation">:</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>ValueError<span class="token punctuation">)</span>
<span class="token keyword">print</span> <span class="token punctuation">(</span>e <span class="token punctuation">)</span>
<span class="token comment"># 调用函数</span>
temp_convert<span class="token punctuation">(</span><span class="token string">"xyz"</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li></ul></pre>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL1pob3V6aXF1blpaUVpaUS9waWNCZWQvbWFzdGVyLzIwMTkwNzAxMjIzOTM4LnBuZw" alt=""></p>
<h2><a name="t11"></a><a id="_323"></a>六、创建自己的异常</h2>
<p>一个异常可以是一个字符串,类或对象。 Python的内核提供的异常,大多数都是实例化的类,这是一个类的实例的参数。</p>
<p>通过创建一个新的异常类,程序可以命名它们自己的异常。异常应该是典型的继承自Exception类,通过直接或间接的方式。你可以通过创建一个新的异常类来拥有自己的异常。异常类继承自 Exception 类。</p>
<p>在这个例子中,类 Exception 默认的 __init__() 被覆盖。</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token comment">#自定义异常</span>
<span class="token keyword">class</span> <span class="token class-name">LengthRequiredException</span><span class="token punctuation">(</span>Exception<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>length<span class="token punctuation">,</span>minLength<span class="token punctuation">)</span><span class="token punctuation">:</span>
Exception<span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>self<span class="token punctuation">)</span>
self<span class="token punctuation">.</span>length <span class="token operator">=</span> length
self<span class="token punctuation">.</span>minLength <span class="token operator">=</span> minLength
<span class="token comment">#引发自定义的异常</span>
l <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>
minLength <span class="token operator">=</span> <span class="token number">6</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
<span class="token keyword">raise</span> LengthRequiredException<span class="token punctuation">(</span><span class="token builtin">len</span><span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">,</span>minLength<span class="token punctuation">)</span>
<span class="token keyword">except</span> IndexError<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"index out of bounds"</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> LengthRequiredException <span class="token keyword">as</span> e<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"Length not fit :length is %d required %d"</span> <span class="token operator">%</span><span class="token punctuation">(</span>e<span class="token punctuation">.</span>length<span class="token punctuation">,</span>e<span class="token punctuation">.</span>minLength<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"no exception was raised"</span><span class="token punctuation">)</span>
<span class="token keyword">finally</span><span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"finally will be execute"</span><span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li></ul></pre>
<p><img src="https://imgconvert.csdnimg.cn/aHR0cHM6Ly9yYXcuZ2l0aHVidXNlcmNvbnRlbnQuY29tL1pob3V6aXF1blpaUVpaUS9waWNCZWQvbWFzdGVyLzIwMTkwNzAxMjI0MDA5LnBuZw" alt=""></p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">class</span> <span class="token class-name">OutOfRangeException</span><span class="token punctuation">(</span>Exception<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>errMsg<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>msg <span class="token operator">=</span> errMsg
<span class="token keyword">def</span> <span class="token function">__str__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> self<span class="token punctuation">.</span>msg
<span class="token keyword">class</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token builtin">object</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token boolean">None</span>
self<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token boolean">None</span>
<span class="token keyword">def</span> <span class="token function">setAge</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>age<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> age <span class="token operator"><</span> <span class="token number">0</span> <span class="token operator">or</span> age <span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">:</span>
<span class="token keyword">raise</span> OutOfRangeException<span class="token punctuation">(</span><span class="token string">"年龄应该在0-100之间!"</span><span class="token punctuation">)</span>
self<span class="token punctuation">.</span>age <span class="token operator">=</span> age
<span class="token keyword">def</span> <span class="token function">setName</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>name<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>name <span class="token operator">=</span> name
<span class="token keyword">def</span> <span class="token function">__str__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token string">"name:{} age:{}"</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>name<span class="token punctuation">,</span>self<span class="token punctuation">.</span>age<span class="token punctuation">)</span>
person <span class="token operator">=</span> Person<span class="token punctuation">(</span><span class="token punctuation">)</span>
person<span class="token punctuation">.</span>setName<span class="token punctuation">(</span><span class="token string">"Edward"</span><span class="token punctuation">)</span>
person<span class="token punctuation">.</span>setAge<span class="token punctuation">(</span><span class="token number">80</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
person<span class="token punctuation">.</span>setAge<span class="token punctuation">(</span><span class="token number">101</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> OutOfRangeException <span class="token keyword">as</span> ex<span class="token punctuation">:</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>ex<span class="token punctuation">)</span>
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li></ul></pre>
<p><img src="https://img-blog.csdnimg.cn/20190713204154239.png" alt="[外链图片转存失败(img-XkWLo5lC-1563021454254)(https://raw.githubusercontent.com/ZhouziqunZZQZZQ/picBed/master/20190701224034.png)]"></p>
<p>当创建一个模块有可能抛出多种不同的异常时,一种通常的做法是为这个包建立一个基础异常类,然后基于这个基础类为不同的错误情况创建不同的子类:</p>
<pre class="prettyprint"><code class="prism language-python has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;"><span class="token keyword">class</span> <span class="token class-name">Error</span><span class="token punctuation">(</span>Exception<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">"""Base class for exceptions in this module."""</span>
<span class="token keyword">pass</span>
<span class="token keyword">class</span> <span class="token class-name">InputError</span><span class="token punctuation">(</span>Error<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">"""Exception raised for errors in the input.
Attributes:
expression -- input expression in which the error occurred
message -- explanation of the error
"""</span>
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> expression<span class="token punctuation">,</span> message<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>expression <span class="token operator">=</span> expression
self<span class="token punctuation">.</span>message <span class="token operator">=</span> message
<span class="token keyword">class</span> <span class="token class-name">TransitionError</span><span class="token punctuation">(</span>Error<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">"""Raised when an operation attempts a state transition that's not
allowed.
Attributes:
previous -- state at beginning of transition
next -- attempted new state
message -- explanation of why the specific transition is not allowed
"""</span>
<span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> previous<span class="token punctuation">,</span> <span class="token builtin">next</span><span class="token punctuation">,</span> message<span class="token punctuation">)</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>previous <span class="token operator">=</span> previous
self<span class="token punctuation">.</span><span class="token builtin">next</span> <span class="token operator">=</span> <span class="token builtin">next</span>
self<span class="token punctuation">.</span>message <span class="token operator">=</span> message
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li><li style="color: rgb(153, 153, 153);">10</li><li style="color: rgb(153, 153, 153);">11</li><li style="color: rgb(153, 153, 153);">12</li><li style="color: rgb(153, 153, 153);">13</li><li style="color: rgb(153, 153, 153);">14</li><li style="color: rgb(153, 153, 153);">15</li><li style="color: rgb(153, 153, 153);">16</li><li style="color: rgb(153, 153, 153);">17</li><li style="color: rgb(153, 153, 153);">18</li><li style="color: rgb(153, 153, 153);">19</li><li style="color: rgb(153, 153, 153);">20</li><li style="color: rgb(153, 153, 153);">21</li><li style="color: rgb(153, 153, 153);">22</li><li style="color: rgb(153, 153, 153);">23</li><li style="color: rgb(153, 153, 153);">24</li><li style="color: rgb(153, 153, 153);">25</li><li style="color: rgb(153, 153, 153);">26</li><li style="color: rgb(153, 153, 153);">27</li><li style="color: rgb(153, 153, 153);">28</li><li style="color: rgb(153, 153, 153);">29</li><li style="color: rgb(153, 153, 153);">30</li></ul></pre>
<h2><a name="t12"></a><a id="_428"></a>七、常用模块</h2>
<p>同样的例子也可以写成如下方式:</p>
<pre class="prettyprint"><code class="has-numbering" onclick="mdcp.copyCode(event)" style="position: unset;">try:
fh = open("testfile", "w")
try:
fh.write("这是一个测试文件,用于测试异常!!")
finally:
print "关闭文件"
fh.close()
except IOError:
print "Error: 没有找到文件或读取文件失败"
<div class="hljs-button {2}" data-title="复制"></div></code><ul class="pre-numbering" style=""><li style="color: rgb(153, 153, 153);">1</li><li style="color: rgb(153, 153, 153);">2</li><li style="color: rgb(153, 153, 153);">3</li><li style="color: rgb(153, 153, 153);">4</li><li style="color: rgb(153, 153, 153);">5</li><li style="color: rgb(153, 153, 153);">6</li><li style="color: rgb(153, 153, 153);">7</li><li style="color: rgb(153, 153, 153);">8</li><li style="color: rgb(153, 153, 153);">9</li></ul></pre>
<p>当在try块中抛出一个异常,立即执行finally块代码。</p>
<p>finally块中的所有语句执行后,异常被再次触发,并执行except块代码。</p>
<p>参数的内容不同于异常。</p>
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
</div>
'''
markdown = html2text.html2text(html)
print(type(markdown))
print(markdown)
f2 = open('blog.md','w')
f2.write(markdown)
f2.close()