Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 30-07-2009
Member
 
Join Date: May 2008
Posts: 13
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
Reply With Quote
  #2  
Old 30-07-2009
Member
 
Join Date: May 2008
Posts: 41
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
Reply With Quote
  #3  
Old 30-07-2009
Member
 
Join Date: May 2008
Posts: 24
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.
Reply With Quote
  #4  
Old 30-07-2009
Member
 
Join Date: May 2008
Posts: 13
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Help: PHP exceptions handling explanation needed"
Thread Thread Starter Forum Replies Last Post
Exceptions of PHP in set_exception_handler? - Empty Shell - Software Development 5 23-02-2010 02:12 AM
What are Exceptions in PHP 5? DANIEL 602 Software Development 5 23-02-2010 01:35 AM
What is Log .NET Exceptions Agneya Software Development 4 07-02-2010 01:34 AM
PHP Error exceptions nehal_serpa Software Development 3 01-05-2009 06:55 PM
Suggestions needed on handling of suspected invalid OS with crack tool installed jenster Operating Systems 5 17-07-2008 02:12 PM


All times are GMT +5.5. The time now is 04:04 PM.