PHPにおける例外処理とは何ですか?

王林
リリース: 2023-09-17 08:46:01
転載
923 人が閲覧しました

PHPにおける例外処理とは何ですか?

#例外とは、プログラムの実行中に発生する問題です。プログラムの実行中に例外が発生すると、ステートメントに続くコードは実行されず、PHP は最初に一致する catch ブロックを見つけようとします。例外がキャッチされない場合は、PHP 致命的エラーが発行され、「Uncaught Exception」が表示されます。

文法

   try {
      print "this is our try block";
      throw new Exception();
      }catch (Exception $e) {
         print "something went wrong, caught yah! n";
      }finally {
         print "this part is always executed";
      }
ログイン後にコピー

中国語翻訳は次のとおりです:

<?php
   function printdata($data) {
      try {
         //If var is six then only if will be executed
         if($data == 6) {
         // If var is zero then only exception is thrown
         throw new Exception(&#39;Number is six.&#39;);
            echo "</p><p> After throw (It will never be executed)";
         }
      }
      // When Exception has been thrown by try block
         catch(Exception $e){
            echo "</p><p> Exception Caught", $e->getMessage();
         }
      //this block code will always executed.
         finally{
            echo "</p><p> Final block will be always executed";
         }
   }
   // Exception will not be rised here
      printdata(0);
      // Exception will be rised
      printdata(6);

?>
ログイン後にコピー

出力

Final block will be always executed
Exception CaughtNumber is six.
Final block will be always executed
ログイン後にコピー

To To例外を処理するには、プログラム コードを try ブロック内に配置する必要があります。各試行には少なくとも 1 つの対応する catch ブロックが必要です。複数の catch ブロックを使用して、さまざまなカテゴリの例外をキャッチできます。

以上がPHPにおける例外処理とは何ですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:tutorialspoint.com
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!