Help: PHP exceptions handling explanation needed
Hi,
Help: PHP exceptions handling explanation needed
Can someone please explain me the basics & importance of exception handling in PHP? say PHP 5?
I have not learned PHP in details but know some basics only. My friend told me to use exception handling but i don't know how to. I suppose this exception handling is a part of Java language.
Thanks in advance.
Regards,
strangist
Re: Help: PHP exceptions handling explanation needed
What is an Exception
PHP 5: Exception handling is used to change the normal flow of the code execution if a specified error (exceptional) condition occurs. This condition is called an exception.
This is what normally happens when an exception is triggered:
- The current code state is saved
- The code execution will switch to a predefined (custom) exception handler function
- Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code
Different error handling methods:
- Basic use of Exceptions
- Creating a custom exception handler
- Multiple exceptions
- Re-throwing an exception
- Setting a top level exception handler
Basic Use of Exceptions
When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block.
If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message.
Try, throw and catch
Proper exception code should include:
1. Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"
2. Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"
3. Catch - A "catch" block retrieves an exception and creates an object containing the exception information
Lets try to trigger an exception with valid code:
PHP Code:
<?php
//create function with an exception
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try
{
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//catch exception
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
The code above will get an error like this:
Code:
Message: Value must be 1 or below
Re: Help: PHP exceptions handling explanation needed
Error Handling and Logging
This is what you must go through.
Here exception handling is explained in details & with examples.
Exception handling is a must. Either your code fails, or external libraries you’ve grown to accept produce faults outside of your control.
This is an interesting Article on Exception handling
I hope this helps you.
Re: Help: PHP exceptions handling explanation needed
Thanks a lot guys for the explanation & the prompt reply.
I hope these articles on error handling point me in right direction & ill be posting my problems in this section if I have problem understanding any concept.
Thanks again.