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メソッドの説明と例が含まれています。