Maison > développement back-end > tutoriel php > PHP 异常与错误 笔记与总结(17 )像处理错误一样处理 PHP 异常

PHP 异常与错误 笔记与总结(17 )像处理错误一样处理 PHP 异常

WBOY
Libérer: 2016-06-13 12:22:34
original
848 Les gens l'ont consulté

PHP 错误与异常 笔记与总结(17 )像处理异常一样处理 PHP 错误

有两种方式可以在 PHP 中以异常的方式处理错误:

① PHP 内置的 ErrorException 类(也是 Exception 类的子类)

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">function</span> exception_error_handler(<span style="color: #800080;">$errno</span>, <span style="color: #800080;">$errstr</span>, <span style="color: #800080;">$errfile</span>, <span style="color: #800080;">$errline</span><span style="color: #000000;">){</span><span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> <span style="font-size: 16px;"><strong><span style="color: #ff0000;">ErrorException</span></strong></span>(<span style="color: #800080;">$errstr</span>, 0, <span style="color: #800080;">$errno</span>, <span style="color: #800080;">$errfile</span>, <span style="color: #800080;">$errline</span><span style="color: #000000;">);</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span> <span style="color: #ff0000; font-size: 16px;"><strong>set_error_handler('exception_error_handler');</strong></span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #0000ff;">try</span><span style="color: #000000;">{</span><span style="color: #008080;"> 9</span>     <span style="color: #008000;">//</span><span style="color: #008000;">出发异常</span><span style="color: #008080;">10</span>     <span style="color: #0000ff;">echo</span> <span style="color: #008080;">gettype</span><span style="color: #000000;">();</span><span style="color: #008080;">11</span> }<span style="color: #0000ff;">catch</span>(<span style="color: #0000ff;">Exception</span> <span style="color: #800080;">$e</span><span style="color: #000000;">){</span><span style="color: #008080;">12</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage();</span><span style="color: #008080;">13</span> }
Copier après la connexion

输出:

用过这种方式把警告信息以异常信息输出出来了。

 

 

自定义异常类来处理 PHP 中的错误 

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span> ErrorToException <span style="color: #0000ff;">extends</span> <span style="color: #0000ff;">Exception</span><span style="color: #000000;">{</span><span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> handler(<span style="color: #800080;">$errno</span>, <span style="color: #800080;">$errstr</span><span style="color: #000000;">){</span><span style="color: #008080;"> 4</span>         <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> self(<span style="color: #800080;">$errstr</span>, <span style="color: #800080;">$errno</span><span style="color: #000000;">);</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">    }</span><span style="color: #008080;"> 6</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">array</span>('ErrorToException', 'handler'<span style="color: #000000;">));</span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #0000ff;">try</span><span style="color: #000000;">{</span><span style="color: #008080;">11</span>     <span style="color: #008000;">//</span><span style="color: #008000;">触发错误,未定义的变量,产生通知级别的错误</span><span style="color: #008080;">12</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$test</span><span style="color: #000000;">;</span><span style="color: #008080;">13</span> }<span style="color: #0000ff;">catch</span>(ErrorToException <span style="color: #800080;">$e</span><span style="color: #000000;">){</span><span style="color: #008080;">14</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage();</span><span style="color: #008080;">15</span> }
Copier après la connexion

输出:

 

还可以使用 set_error_handler 设定错误级别:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span> ErrorToException <span style="color: #0000ff;">extends</span> <span style="color: #0000ff;">Exception</span><span style="color: #000000;">{</span><span style="color: #008080;"> 3</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> handler(<span style="color: #800080;">$errno</span>, <span style="color: #800080;">$errstr</span><span style="color: #000000;">){</span><span style="color: #008080;"> 4</span>         <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> self(<span style="color: #800080;">$errstr</span>, <span style="color: #800080;">$errno</span><span style="color: #000000;">);</span><span style="color: #008080;"> 5</span> <span style="color: #000000;">    }</span><span style="color: #008080;"> 6</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 7</span> <span style="color: #008080;"> 8</span> <span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">array</span>('ErrorToException', 'handler'<span style="color: #000000;">));</span><span style="color: #008080;"> 9</span> <span style="color: #008000;">//</span><span style="color: #008000;">设定错误级别,例如只有警告级别的错误才以异常的形式进行捕获</span><span style="color: #008080;">10</span> <span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">array</span>('ErrorToException', 'handler'), <span style="color: #ff00ff;">E_USER_WARNING</span>|<span style="color: #ff00ff;">E_WARNING</span><span style="color: #000000;">);</span><span style="color: #008080;">11</span> <span style="color: #008080;">12</span> <span style="color: #0000ff;">try</span><span style="color: #000000;">{</span><span style="color: #008080;">13</span>     <span style="color: #008000;">//</span><span style="color: #008000;">触发错误,未定义的变量,产生通知级别的错误</span><span style="color: #008080;">14</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$test</span><span style="color: #000000;">;</span><span style="color: #008080;">15</span> }<span style="color: #0000ff;">catch</span>(ErrorToException <span style="color: #800080;">$e</span><span style="color: #000000;">){</span><span style="color: #008080;">16</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage();</span><span style="color: #008080;">17</span> }
Copier après la connexion

输出:

也就是说,通知级别的错误不交给自定义异常类来处理。

 

trigger_error 也能触发异常:

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;">header</span>('content-type:text/html; charset=utf-8'<span style="color: #000000;">);</span><span style="color: #008080;"> 3</span> <span style="color: #008080;"> 4</span> <span style="color: #0000ff;">class</span> ErrorToException <span style="color: #0000ff;">extends</span> <span style="color: #0000ff;">Exception</span><span style="color: #000000;">{</span><span style="color: #008080;"> 5</span>     <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span> handler(<span style="color: #800080;">$errno</span>, <span style="color: #800080;">$errstr</span><span style="color: #000000;">){</span><span style="color: #008080;"> 6</span>         <span style="color: #0000ff;">throw</span> <span style="color: #0000ff;">new</span> self(<span style="color: #800080;">$errstr</span>, <span style="color: #800080;">$errno</span><span style="color: #000000;">);</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">    }</span><span style="color: #008080;"> 8</span> <span style="color: #000000;">}</span><span style="color: #008080;"> 9</span> <span style="color: #008080;">10</span> <span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">array</span>('ErrorToException', 'handler'<span style="color: #000000;">));</span><span style="color: #008080;">11</span> <span style="color: #008000;">//</span><span style="color: #008000;">设定错误级别,例如至哟哟警告级别的错误才以异常的形式进行捕获</span><span style="color: #008080;">12</span> <span style="color: #008080;">set_error_handler</span>(<span style="color: #0000ff;">array</span>('ErrorToException', 'handler'), <span style="color: #ff00ff;">E_USER_WARNING</span>|<span style="color: #ff00ff;">E_WARNING</span><span style="color: #000000;">);</span><span style="color: #008080;">13</span> <span style="color: #008080;">14</span> <span style="color: #0000ff;">try</span><span style="color: #000000;">{</span><span style="color: #008080;">15</span>     <span style="color: #008000;">//</span><span style="color: #008000;">触发错误,未定义的变量,产生通知级别的错误</span><span style="color: #008080;">16</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$test</span><span style="color: #000000;">;</span><span style="color: #008080;">17</span>     <span style="color: #0000ff;">echo</span> '<hr>'<span style="color: #000000;">;</span><span style="color: #008080;">18</span>     <span style="color: #008000;">//</span><span style="color: #008000;">gettype();</span><span style="color: #008080;">19</span>     <span style="color: #0000ff;">echo</span> '<hr>'<span style="color: #000000;">;</span><span style="color: #008080;">20</span>     <span style="color: #008080;">trigger_error</span>('test', <span style="color: #ff00ff;">E_USER_WARNING</span><span style="color: #000000;">);</span><span style="color: #008080;">21</span> }<span style="color: #0000ff;">catch</span>(ErrorToException <span style="color: #800080;">$e</span><span style="color: #000000;">){</span><span style="color: #008080;">22</span>     <span style="color: #0000ff;">echo</span> '异常'<span style="color: #000000;">;</span><span style="color: #008080;">23</span>     <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$e</span>-><span style="color: #000000;">getMessage();</span><span style="color: #008080;">24</span> }
Copier après la connexion

输出:

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal