pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<small id='XQkCK'></small><noframes id='XQkCK'>

  • <tfoot id='XQkCK'></tfoot>

      1. <legend id='XQkCK'><style id='XQkCK'><dir id='XQkCK'><q id='XQkCK'></q></dir></style></legend>
        • <bdo id='XQkCK'></bdo><ul id='XQkCK'></ul>

        <i id='XQkCK'><tr id='XQkCK'><dt id='XQkCK'><q id='XQkCK'><span id='XQkCK'><b id='XQkCK'><form id='XQkCK'><ins id='XQkCK'></ins><ul id='XQkCK'></ul><sub id='XQkCK'></sub></form><legend id='XQkCK'></legend><bdo id='XQkCK'><pre id='XQkCK'><center id='XQkCK'></center></pre></bdo></b><th id='XQkCK'></th></span></q></dt></tr></i><div class="87xxwmz" id='XQkCK'><tfoot id='XQkCK'></tfoot><dl id='XQkCK'><fieldset id='XQkCK'></fieldset></dl></div>

        為什么以及如何在此示例 PHP 代碼中使用異常?

        Why and how would you use Exceptions in this sample PHP code?(為什么以及如何在此示例 PHP 代碼中使用異常?)
        • <legend id='PLLpz'><style id='PLLpz'><dir id='PLLpz'><q id='PLLpz'></q></dir></style></legend>
          <tfoot id='PLLpz'></tfoot>
            <tbody id='PLLpz'></tbody>

            <i id='PLLpz'><tr id='PLLpz'><dt id='PLLpz'><q id='PLLpz'><span id='PLLpz'><b id='PLLpz'><form id='PLLpz'><ins id='PLLpz'></ins><ul id='PLLpz'></ul><sub id='PLLpz'></sub></form><legend id='PLLpz'></legend><bdo id='PLLpz'><pre id='PLLpz'><center id='PLLpz'></center></pre></bdo></b><th id='PLLpz'></th></span></q></dt></tr></i><div class="ahkiem2" id='PLLpz'><tfoot id='PLLpz'></tfoot><dl id='PLLpz'><fieldset id='PLLpz'></fieldset></dl></div>

            • <small id='PLLpz'></small><noframes id='PLLpz'>

                  <bdo id='PLLpz'></bdo><ul id='PLLpz'></ul>
                  本文介紹了為什么以及如何在此示例 PHP 代碼中使用異常?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我一直想知道為什么要在我的 PHP 中使用異常.我們來看一個簡單的例子:

                  I've been wondering why would I use Exceptions in my PHP. Let's take a look at a simple example:

                  class Worker
                  {
                   public function goToWork()
                   {
                    return $isInThatMood ?
                    // Okay, I'll do it.
                    true : 
                    // In your dreams...
                    false;
                   }
                  }
                  
                  $worker = new Worker;
                  if (!$worker->goToWork())
                  {
                   if (date('l',time()) == 'Sunday')
                    echo "Fine, you don't have to work on Sundays...";
                   else
                    echo "Get your a** back to work!";
                  }
                  else
                   echo "Good.";
                  

                  我有理由對那種代碼使用異常嗎?為什么?代碼將如何構建?

                  Is there a reason for me to use Exceptions for that kind of code? Why? How would the code be built?

                  還有可能產生錯誤的代碼:

                  And what about code that may produce errors:

                  class FileOutputter
                  {
                   public function outputFile($file)
                   {
                    if (!file_exists($file))
                     return false;
                    return file_get_contents($file);
                   }
                  }
                  

                  為什么我會在上述情況下使用異常?我有一種感覺,異常可以幫助您識別問題的類型,這是真的嗎?

                  Why would I use Exceptions in the above case? I have a feeling that Exceptions help you to recognize the type of the problem, which occured, true?

                  那么,我在這段代碼中是否適當地使用了異常:

                  So, am I using Exceptions appropriately in this code:

                  class FileOutputter
                  {
                   public function outputFile($file)
                   {
                    if (!file_exists($file))
                     return throw new Exception("File not found.",123);
                    try
                    {
                     $contents = file_get_contents($file);
                    }
                    catch (Exception $e)
                    {
                     return $e;
                    }
                    return $contents;
                   }
                  }
                  

                  或者是窮?現在底層代碼可以做到這一點:

                  Or is that poor? Now the underlying code can do this:

                  $fo = new FileOutputter;
                  try
                  {
                   $fo->outputFile("File.extension");
                  }
                  catch (Exception $e)
                  {
                   // Something happened, we could either display the error/problem directly
                   echo $e->getMessage();
                   // Or use the info to make alternative execution flows
                   if ($e->getCode() == 123) // The one we specified earlier
                    // Do something else now, create "an exception"
                  }
                  

                  還是我完全迷失在這里?

                  Or am I completely lost here?

                  推薦答案

                  我什么時候應該使用異常?

                  您使用異常來表示異常條件;也就是說,阻止方法履行其合同的事情,并且不應該在該級別發生.

                  When should I use an exception?

                  You use an exception to indicate an exceptional condition; that is, something which prevents a method from fulfilling its contract, and which shouldn't have occurred at that level.

                  例如,您可能有一個方法,Record::save(),它將對記錄的更改保存到數據庫中.如果由于某種原因無法做到這一點(例如,發生數據庫錯誤或數據約束被破壞),那么您可以拋出異常以指示失敗.

                  For example, you might have a method, Record::save(), which saves changes to a record into a database. If, for some reason, this can't be done (e.g. a database error occurs, or a data constraint is broken), then you could throw an exception to indicate failure.

                  異常的命名通常表明錯誤的性質,例如,DatabaseException.您可以繼承 Exception 以這種方式創建自定義命名的異常,例如

                  Exceptions are usually named such that they indicate the nature of the error, for example, DatabaseException. You can subclass Exception to create custom-named exceptions in this manner, e.g.

                  class DatabaseException extends Exception {}
                  

                  (當然,您可以利用繼承為該異常提供一些額外的診斷信息,例如連接詳細信息或數據庫錯誤代碼.)

                  (Of course, you could take advantage of inheritance to give this exception some additional diagnostic information, such as connection details or a database error code, for example.)

                  再看一個例子;一種檢查文件存在的方法.如果文件不存在,這應該拋出異常,因為該方法的目的是執行上述檢查.但是,打開文件并執行某些處理的方法可能拋出異常,因為該文件應該存在,等等.

                  Consider a further example; a method which checks for file existence. This should probably not throw an exception if the file doesn't exist, since the purpose of the method was to perform said check. However, a method which opens a file and performs some processing could throw an exception, since the file was expected to exist, etc.

                  最初,有時并不總是很清楚什么是例外,什么時候不是例外.像大多數事情一樣,隨著時間的推移,經驗會告訴你什么時候應該和不應該拋出異常.

                  Initially, it's not always clear when something is and isn't exceptional. Like most things, experience will teach you, over time, when you should and shouldn't throw an exception.

                  異常的有用之處在于它們會立即跳出當前方法并向上調用調用堆棧,直到它們被捕獲和處理,這意味著您可以將錯誤處理邏輯移到更高的位置,盡管理想情況下,不要太高.

                  The useful thing about exceptions is that they immediately leap out of the current method and head up the call stack until they're caught and handled, which means you can move error-handling logic higher up, although ideally, not too high.

                  通過使用清晰的機制來處理失敗案例,當發生不好的事情時,您會自動啟動錯誤處理代碼,這意味著您可以避免處理必須檢查的各種魔法哨兵值,或者更糟的是,用于區分一堆不同可能性的全局錯誤標志.

                  By using a clear mechanism to deal with failure cases, you automatically kick off the error handling code when something bad happens, which means you can avoid dealing with all sorts of magic sentinel values which have to be checked, or worse, a global error flag to distinguish between a bunch of different possibilities.

                  這篇關于為什么以及如何在此示例 PHP 代碼中使用異常?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  MySQLi prepared statement amp; foreach loop(MySQLi準備好的語句amp;foreach 循環)
                  Is mysqli_insert_id() gets record from whole server or from same user?(mysqli_insert_id() 是從整個服務器還是從同一用戶獲取記錄?)
                  PHP MySQLi doesn#39;t recognize login info(PHP MySQLi 無法識別登錄信息)
                  mysqli_select_db() expects exactly 2 parameters(mysqli_select_db() 需要 2 個參數)
                  Php mysql pdo query: fill up variable with query result(Php mysql pdo 查詢:用查詢結果填充變量)
                  MySQLI 28000/1045 Access denied for user #39;root#39;@#39;localhost#39;(MySQLI 28000/1045 用戶“root@“localhost的訪問被拒絕)
                  <tfoot id='6H7IM'></tfoot>

                • <small id='6H7IM'></small><noframes id='6H7IM'>

                  • <bdo id='6H7IM'></bdo><ul id='6H7IM'></ul>

                      <legend id='6H7IM'><style id='6H7IM'><dir id='6H7IM'><q id='6H7IM'></q></dir></style></legend>

                            <i id='6H7IM'><tr id='6H7IM'><dt id='6H7IM'><q id='6H7IM'><span id='6H7IM'><b id='6H7IM'><form id='6H7IM'><ins id='6H7IM'></ins><ul id='6H7IM'></ul><sub id='6H7IM'></sub></form><legend id='6H7IM'></legend><bdo id='6H7IM'><pre id='6H7IM'><center id='6H7IM'></center></pre></bdo></b><th id='6H7IM'></th></span></q></dt></tr></i><div class="k8bxvio" id='6H7IM'><tfoot id='6H7IM'></tfoot><dl id='6H7IM'><fieldset id='6H7IM'></fieldset></dl></div>
                              <tbody id='6H7IM'></tbody>
                            主站蜘蛛池模板: 空冷器|空气冷却器|空水冷却器-无锡赛迪森机械有限公司[官网] | 日本东丽膜_反渗透膜_RO膜价格_超滤膜_纳滤膜-北京东丽阳光官网 日本细胞免疫疗法_肿瘤免疫治疗_NK细胞疗法 - 免疫密码 | LED灯杆屏_LED广告机_户外LED广告机_智慧灯杆_智慧路灯-太龙智显科技(深圳)有限公司 | sus630/303cu不锈钢棒,440C/430F/17-4ph不锈钢研磨棒-江苏德镍金属科技有限公司 | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 深圳标识制作公司-标识标牌厂家-深圳广告标识制作-玟璟广告-深圳市玟璟广告有限公司 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 上海诺狮景观规划设计有限公司 | 沈阳建筑设计公司_加固改造设计_厂房设计_设计资质加盟【金辉设计】 | 上海租车公司_上海包车_奔驰租赁_上海商务租车_上海谐焕租车 | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 驾驶人在线_专业学车门户网站 | 注塑机-压铸机-塑料注塑机-卧式注塑机-高速注塑机-单缸注塑机厂家-广东联升精密智能装备科技有限公司 | 卫浴散热器,卫浴暖气片,卫生间背篓暖气片,华圣格浴室暖气片 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 电竞馆加盟,沈阳网吧加盟费用选择嘉棋电竞_售后服务一体化 | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | 深圳高新投三江工业消防解决方案提供厂家_服务商_园区智慧消防_储能消防解决方案服务商_高新投三江 | 知网论文检测系统入口_论文查重免费查重_中国知网论文查询_学术不端检测系统 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 玻纤土工格栅_钢塑格栅_PP焊接_单双向塑料土工格栅_复合防裂布厂家_山东大庚工程材料科技有限公司 | 低温柔性试验仪-土工布淤堵-沥青车辙试验仪-莱博特(天津)试验机有限公司 | 回转支承-转盘轴承-回转驱动生产厂家-洛阳隆达轴承有限公司 | 上海办公室装修_上海店铺装修公司_厂房装潢设计_办公室装修 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 高精度电阻回路测试仪-回路直流电阻测试仪-武汉特高压电力科技有限公司 | 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 深圳工程师职称评定条件及流程_深圳职称评审_职称评审-职称网 | 深圳市源和塑胶电子有限公司-首页 | 卷筒电缆-拖链电缆-特种柔性扁平电缆定制厂家「上海缆胜」 | 定时排水阀/排气阀-仪表三通旋塞阀-直角式脉冲电磁阀-永嘉良科阀门有限公司 | 吊篮式|移动式冷热冲击试验箱-二槽冷热冲击试验箱-广东科宝 | 烟台条码打印机_烟台条码扫描器_烟台碳带_烟台数据采集终端_烟台斑马打印机-金鹏电子-金鹏电子 | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 连续油炸机,全自动油炸机,花生米油炸机-烟台茂源食品机械制造有限公司 | 电动垃圾车,垃圾清运车-江苏速利达机车有限公司 | 山东成考网-山东成人高考网| 滁州高低温冲击试验箱厂家_安徽高低温试验箱价格|安徽希尔伯特 | 冷藏车-东风吸污车-纯电动环卫车-污水净化车-应急特勤保障车-程力专汽厂家-程力专用汽车股份有限公司销售二十一分公司 | 烘干设备-热泵烘干机_广东雄贵能源设备有限公司 |