在程序开发中,错误处理这一块是非常重要的,今天本文章就来告诉他关于在php开发中,错误处理函数并且举例说明错误处理的重要性。
1、内置异常处理类、2、捕获并处理异常的示例、3、exception类的成员函数getmessage()、4、exception类的成员函数getfile()、5、exception类的成员函数getline()、6、显示警告或错误信息、7、自定义错误处理函数。
1,内置异常处理类
- class exception
- {
- protected $message = 'unknown exception'; // 异常信息
- protected $code = 0; // 用户自定义异常代码
- protected $file; // 发生异常的文件名
- protected $line; // 发生异常的代码行号
- function __construct($message = null, $code = 0);
- final function getmessage(); // 返回异常信息
- final function getcode(); // 返回异常代码
- final function getfile(); // 返回发生异常的文件名
- final function getline(); // 返回发生异常的代码行号
- final function gettrace(); // backtrace() 数组
- final function gettraceasstring(); // 已格成化成字符串的 gettrace() 信息
- function __tostring(); // 可输出的字符串
- }
2、捕获并处理异常的示例
- try
- {
- $error = '抛出异常信息,并且跳出try块<br/>';
- if(is_dir('./test'))
- {
- echo '检测到../ch16是一个目录';
- echo '<br/>';
- echo '可能继续做其他一些操作';
- echo '<br/>';
- echo '….';
- echo '<br/>';
- }
- else
- {
- throw new exception($error,12345);
- }
- echo '上面throw异常的话,这行代码不会执行,转而执行catch块<br/>';
- }
- catch(exception $e)
- {
- echo '捕获异常: ' . $e->getmessage() . "<br/>错误代码:" . $e->getcode().'<br/>'; //显示$error和123456
- echo '<br/>';
- }
- echo '继续执行';
3、exception类的成员函数getmessage()
- $file = './test/readme.txt';
- try
- {
- if(is_dir($file))
- {
- echo '检测到目录';
- }
- else
- {
- //创建异常对象,错误信息将有exception类的成员函数getmessage()返回
- throw new exception('未找到该目录或文件');
- }
- }
- catch(exception $e)
- {
- echo '捕获异常: ' . $e->getmessage();
- echo '<br/>===============================';
- echo '<br/>';
- }
- echo '程序执行完毕';
4、exception类的成员函数getfile()
- $file = './test/readme.txt';
- try
- {
- if(is_dir($file))
- {
- echo '检测到目录';
- }
- else
- {
- //创建异常对象,错误信息将有exception类的成员函数getmessage()返回
- throw new exception('未找到该目录或文件');
- }
- }
- catch(exception $e)
- {
- echo '捕获异常: ' . $e->getmessage();
- echo '<br/><br/>';
- echo '错误所在文件:'. $e->getfile();
- echo '<br/>===============================';
- echo '<br/>';
- }
- echo '程序执行完毕';
5、exception类的成员函数getline()
- $file = './test/readme.txt';
- try
- {
- if(is_dir($file))
- {
- echo '检测到目录';
- }
- else
- {
- //创建异常对象,错误信息将有exception类的成员函数getmessage()返回
- throw new exception('未找到该目录或文件');
- }
- }
- catch(exception $e)
- {
- echo '捕获异常: ' . $e->getmessage();
- echo '<br/><br/>';
- echo '错误所在文件:'. $e->getfile();
- echo '<br/><br/>';
- echo '错误所在行号:'. $e->getline();
- echo '<br/>===============================';
- echo '<br/>';
- }
- echo '程序执行完毕';
6、显示警告或错误信息
- error_reporting(e_warning | e_error); //显示警告和错误信息
- echo $uvar; //这里产生注意信息,但不会显示出来
- callfunc(); //这里产生一个致命错误,并且会显示出来
- // 7、自定义错误处理函数。
- set_error_handler('myhandler'); //自定义错误处理函数
- function myhandler($code, $msg, $file, $line)
- {
- echo "<br/>";
- echo "程序 <b>$file</b> 执行过程中,在第 <b>$line</b> 行,产生一个错误。";
- echo "<br/>";
- echo "错误代码为:<b>$code</b>, 错误的原因是: <b>$msg</b>";
- }
- echo $uvar;
波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
波比源码 » php 错误处理与异常处理方法与实例教程
波比源码 » php 错误处理与异常处理方法与实例教程
purchase levaquin online cheap levofloxacin cheap
brand avodart celecoxib 200mg price ondansetron buy online
buy spironolactone 100mg without prescription propecia brand diflucan 200mg usa
ampicillin price buy generic cephalexin 125mg buy erythromycin 500mg generic
fildena 50mg cost order robaxin 500mg online order robaxin
order sildenafil pill buy sildalis without prescription oral estradiol 1mg
buy lamictal 200mg online buy retin for sale tretinoin usa
tadalis buy online diclofenac 50mg drug voltaren 50mg usa
buy tadalafil 5mg generic cialis without a prescription cheap sildenafil without prescription
purchase modafinil sale buy acetazolamide pills order diamox 250 mg
buy doxycycline pills brand clomiphene 100mg lasix 40mg pill
order ramipril generic buy generic avapro 300mg oral astelin
clonidine 0.1mg brand clonidine pills buy spiriva sale
buspirone 10mg pill brand buspar order ditropan 5mg without prescription
terazosin 5mg oral purchase sulfasalazine pill buy azulfidine 500 mg sale
order benicar pill purchase verapamil pills cost acetazolamide 250mg
buy prograf generic prograf order order urso 150mg
buy zyban 150 mg online cheap order cetirizine 5mg for sale order seroquel 100mg without prescription
order zoloft 100mg for sale buy kamagra 100mg for sale viagra 50mg canada
order salbutamol 100mcg protonix ca viagra price
cost cialis 20mg Free trial of cialis order sildenafil
order naltrexone 50 mg sale abilify us brand aripiprazole 20mg
buy dapsone 100mg pill buy dapsone 100mg online purchase perindopril generic
cost medroxyprogesterone 10mg medroxyprogesterone 5mg cheap cyproheptadine 4mg canada
provigil uk stromectol 12mg for sale stromectol 6 mg
order nootropil online cheap order generic piracetam 800mg sildenafil 50mg price
generic furosemide 100mg doxycycline 200mg pill plaquenil 400mg brand
sporanox 100mg price brand tinidazole 500mg tindamax ca
cheap norvasc 5mg viagra 50mg cheap tadalafil without prescription
cost clozaril 100mg order clozaril 50mg pills order decadron 0,0,5 mg for sale
online assignment help that roulette casino slot
triamcinolone 4mg us triamcinolone brand desloratadine order online
buy generic cialis 20mg viagra 150mg for sale sildenafil 50mg oral
allopurinol 300mg us generic crestor buy zetia 10mg sale
buy methotrexate 5mg online cheap coumadin 5mg pills reglan price
motilium oral domperidone for sale online cheap flexeril
order ozobax pill buy zanaflex generic order toradol online
imitrex canada sumatriptan ca purchase dutasteride for sale
buy colchicine 0.5mg online casino games free online slot machines real money
money poker online online casino no deposit bonus poker online for fun
purchase flomax sale buy zofran 4mg aldactone 100mg cost
order zocor for sale propecia drug generic finasteride 1mg
metronidazole uk order generic augmentin buy bactrim 960mg pill
buy generic diflucan 200mg diflucan 100mg generic viagra 50mg canada
buy sildenafil 100mg without prescription cialis cheap overnight cialis delivery
pala casino online online poker order cialis 20mg
order generic deltasone amoxicillin online buy buy amoxicillin 500mg
best online casino usa cialis 20mg drug brand tadalafil 40mg
generic zithromax 500mg prednisolone 10mg usa neurontin 100mg brand
lasix 40mg uk order plaquenil 200mg sale buy plaquenil 400mg pill
fildena 100mg cheap order sildenafil 50mg online cheap rhinocort nasal spray
buy tretinoin cream buy tadalafil 20mg sale avanafil 100mg cost
order tadalafil buy tadalafil 20mg indocin drug
terbinafine 250mg cheap terbinafine generic buy trimox online cheap
order singulair 10mg generic singulair 5mg over the counter buy viagra 50mg generic
doxycycline 100mg oral doxycycline 200mg without prescription buy clindamycin
mesalamine for sale online azelastine sale brand avapro 150mg
digoxin order online lanoxin 250mg us order molnunat online
cheap carvedilol 25mg buy elavil 50mg pills order amitriptyline 10mg sale
order amoxicillin 1000mg oral stromectol 12mg ivermectin cost uk
buy alendronate 35mg online furadantin without prescription order generic motrin
indomethacin 50mg pills indocin 75mg capsule order cenforce 100mg online cheap
glycomet 500mg uk buy nolvadex 10mg generic nolvadex 10mg ca
where to buy ed pills online purchase sildenafil generic buy proscar sale
isotretinoin 10mg usa deltasone 5mg usa buy ampicillin online
ivermectin 12mg without a doctor prescription deltasone order buy prednisone 5mg online cheap
order prednisolone 20mg pills buy furosemide 100mg for sale purchase lasix online cheap
vibra-tabs medication cost vardenafil buy zovirax 400mg
omnicef 300mg pill pantoprazole over the counter buy protonix for sale
ditropan 2.5mg us order trileptal generic buy trileptal generic
buy simvastatin 20mg generic sildalis uk buy sildalis pills
warfarin for sale zyloprim oral order zyloprim pills