PHPチュートリアル

PHPホーム PHPイントロ PHPインストール PHP構文 PHPコメント PHP変数 PHPエコー/印刷 PHPデータ型 PHP文字列 PHP番号 PHP数学 PHP定数 PHP演算子 PHP If ... Else ... Elseif PHPスイッチ PHPループ PHP関数 PHP配列 PHPスーパーグローバル PHP正規表現

PHPフォーム

PHPフォーム処理 PHPフォームの検証 PHPフォームが必要 PHPフォームのURL / Eメール PHPフォームの完了

PHP Advanced

PHPの日付と時刻 PHPインクルード PHPファイルの処理 PHPファイルのオープン/読み取り PHPファイルの作成/書き込み PHPファイルのアップロード PHPクッキー PHPセッション PHPフィルター PHPフィルターアドバンスト PHPコールバック関数 PHP JSON PHPの例外

PHPOOP _

PHPOOPとは PHPクラス/オブジェクト PHPコンストラクター PHPデストラクタ PHPアクセス修飾子 PHPの継承 PHP定数 PHP抽象クラス PHPインターフェース PHPの特性 PHP静的メソッド PHPの静的プロパティ PHP名前空間 PHPIterables

MySQLデータベース

MySQLデータベース MySQLコネクト MySQL Create DB MySQLテーブルの作成 MySQLの挿入データ MySQLは最後のIDを取得します MySQL Insert Multiple MySQLを準備しました MySQL Select Data MySQL Where MySQL Order By MySQLデータの削除 MySQLアップデートデータ MySQL制限データ

PHP XML

PHPXMLパーサー PHPSimpleXMLパーサー PHPSimpleXML-取得 PHP XMLExpat PHP XML DOM

PHP -AJAX

AJAXイントロ AJAX PHP AJAXデータベース AJAX XML AJAXライブ検索 AJAXポール

PHPの

PHPの例 PHPコンパイラ PHPクイズ PHP演習 PHP証明書

PHPリファレンス

PHPの概要 PHP配列 PHPカレンダー PHPの日付 PHPディレクトリ PHPエラー PHP例外 PHPファイルシステム PHPフィルター PHP FTP PHP JSON PHPキーワード PHP Libxml PHPメール PHP数学 PHPその他 PHP MySQLi PHPネットワーク PHP出力制御 PHP正規表現 PHP SimpleXML PHPストリーム PHP文字列 PHP変数の処理 PHPXMLパーサー PHP Zip PHPタイムゾーン

PHPの例外


例外とは何ですか?

例外は、PHPスクリプトのエラーまたは予期しない動作を説明するオブジェクトです。

例外は、多くのPHP関数およびクラスによってスローされます。

ユーザー定義の関数とクラスも例外をスローする可能性があります。

例外は、関数が使用できないデータに遭遇したときに関数を停止するための良い方法です。


例外をスローする

このthrowステートメントにより、ユーザー定義の関数またはメソッドが例外をスローできます。例外がスローされた場合、それに続くコードは実行されません。

例外がキャッチされない場合、「UncaughtException」メッセージで致命的なエラーが発生します。

例外をキャッチせずにスローしてみましょう。

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

echo divide(5, 0);
?>

結果は次のようになります。

Fatal error: Uncaught Exception: Division by zero in C:\webfolder\test.php:4
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4

try ... catchステートメント

try...catch上記の例のエラーを回避するために、ステートメントを使用して 例外をキャッチし、プロセスを続行できます。

構文

try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
}

例外がスローされたときにメッセージを表示します。

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide.";
}
?>

catchブロックは、キャッチする必要のある例外のタイプと、例外へのアクセスに使用できる変数の名前を示します。上記の例では、例外のタイプはでExceptionあり、変数名は$eです。


try ... catch ... finallyステートメント

このtry...catch...finallyステートメントは、例外をキャッチするために使用できます。ブロック内のコードは finally、例外がキャッチされたかどうかに関係なく常に実行されます。finallyが存在する場合 、catchブロックはオプションです。

構文

try {
  code that can throw exceptions
} catch(Exception $e) {
  code that runs when an exception is caught
} finally {
  code that always runs regardless of whether an exception was caught
}

例外がスローされたときにメッセージを表示してから、プロセスが終了したことを示します。

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide. ";
} finally {
  echo "Process complete.";
}
?>

例外がキャッチされなかった場合でも文字列を出力します。

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} finally {
  echo "Process complete.";
}
?>

例外オブジェクト

例外オブジェクトには、関数で発生したエラーまたは予期しない動作に関する情報が含まれています。

構文

new Exception(message, code, previous)

パラメータ値

Parameter Description
message Optional. A string describing why the exception was thrown
code Optional. An integer that can be used used to easily distinguish this exception from others of the same type
previous Optional. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter

メソッド

例外をキャッチする場合、次の表に、例外に関する情報を取得するために使用できるいくつかのメソッドを示します。

Method Description
getMessage() Returns a string describing why the exception was thrown
getPrevious() If this exception was triggered by another one, this method returns the previous exception. If not, then it returns null
getCode() Returns the exception code
getFile() Returns the full path of the file in which the exception was thrown
getLine() Returns the line number of the line of code which threw the exception

スローされた例外に関する情報を出力します。

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero", 1);
  }
  return $dividend / $divisor;
}

try {
  echo divide(5, 0);
} catch(Exception $ex) {
  $code = $ex->getCode();
  $message = $ex->getMessage();
  $file = $ex->getFile();
  $line = $ex->getLine();
  echo "Exception thrown in $file on line $line: [Code $code]
  $message";
}
?>

完全な例外リファレンス

完全なリファレンスについては、完全なPHP例外リファレンスを参照してください。

このリファレンスには、すべてのExceptionメソッドの説明と例が含まれています。