Throw - This is how you trigger an exception. Each "throw" must have at least one "catch".
Catch - A "catch" block retrieves an exception and creates an object containing the exception information.
When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ...
- An exception can be thrown, and caught ("catched") within PHP. Code may be surrounded in a try block.
- Each try must have at least one corresponding catch block. Multiple catch blocks can be used to catch different classes of exceptions.
- Exceptions can be thrown (or re-thrown) within a catch block.
refer http://www.tutorialspoint.com/php/php_error_handling.htm
A simple example on Exception Handling in PHP | try | throw | catch
<?php
function displayDetails($text){
if($text<=10){
throw new Exception("Value should be greater than 10.");
}else{
for($i=0; $i<$text; $i++){
echo $i."<br/>";
}
}
}
?>
<html>
<head>
<title>Check Try | Throw | Catch</title>
</head>
<body>
<form name="frm1">
<input type="text" name="typeValue" value="Type text and hit go"/>
<input type ="submit"/>
</form>
<?php
if($_REQUEST){
if($_REQUEST["typeValue"]){
try{
displayDetails($_REQUEST["typeValue"]);
}
//catch exception
catch(Exception $e){
echo 'Message: ' .$e->getMessage();
}
}
}
?>
</body>
</html>
No comments:
Post a Comment