Showing posts with label switch. Show all posts
Showing posts with label switch. Show all posts

Wednesday, April 14, 2010

Exception Handling

In PHP, we will use exceptions to change the normal flow of a script if a specified error occurs.



Basic Use of Exceptions

In the example below, when an exception is thrown, 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. Now, I will let you try to throw an exception without catching it and see what kind of error that you will get:


(click image to view)

############################################################
The Output:

Fatal error: Uncaught exception 'Exception'
with message 'Value must be 5 or below' in C:\webfolder\index.php:6
Stack trace: #0 C:\webfolder\index.php(12):
checkNum(28) #1 {main} thrown in C:\webfolder\index.php on line 6
############################################################

To avoid the error from the example above, we need to create the proper code to handle an exception. Proper exception code should include:

  • 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".
  • 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.

(click image to view)

############################################################
The Output:

Message: Value must be 5 or below
############################################################

The code above throws an exception and catches it:
  1. The checkNum() function is created. It checks if a number is greater than 10. If it is, an exception is thrown.
  2. The checkNum() function is called in a "try" block.
  3. The exception within the checkNum() function is thrown.
  4. The "catch" block retrives the exception and creates an object ($e) containing the exception information.
  5. The error message from the exception is echoed by calling $e->getMessage() from the exception object.


Creating a Custom Exception Class


(click image to view)

The code above throws an exception and catches it with a custom exception class:
  1. The customException() class is created as an extension of the old exception class. This way it inherits all methods and properties from the old exception class.
  2. The errorMessage() function is created. This function returns an error message if an e-mail address is invalid.
  3. The $email variable is set to a string that is not a valid e-mail address.
  4. The "try" block is executed and an exception is thrown since the e-mail address is invalid.
  5. The "catch" block catches the exception and displays the error message.


Multiple Exception

It is possible for a script to use multiple exceptions to check for multiple conditions. It is possible to use several if..else blocks, a switch, or nest multiple exceptions. These exceptions can use different exception classes and return different error messages:


(click image to view)

(click image to view)

Saturday, April 3, 2010

Switch

Switch Statement

Syntax:


(click image to view)

The syntax above shows a blocks of code to be executed, if the condition is true. But how it works? I'm telling you, first, we have a n expression that is evaluated once. Then, its value will compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. We will use break to prevent the code from running into the next case automatically. If no match found, the default will be used. For instance:


(click image to view)

if...else Statements

We can used conditional statements to perform different actions, based on different conditions. There are 4 conditional statements in PHP. For instances:
  • if - to execute code only a specified condition is TRUE.
  • if..else - to execute code if a condition is TRUE, and another code if a condition is FALSE.
  • if..elseif..else - to select one of several blocks of code to be executed.
  • switch - to select one of many blocks of code to be executed.



The if Statement

The following example will output "Lets go holiday!" if the current day is Saturday:


(click image to view)



The if..else Statement

The following example will output "Lets go holiday!" if the current day is Saturday, otherwise it will output "Lets do our own work!":


(click image to view)

Now, we try to execute more than one line if a condition is true. The lines should be enclosed within curly braces:


(click image to view)



The if..elseif..else Statement

The following example will output "Lets go holiday!" if the current day is Saturday, and "Having fun with friends!" if the current day is Sunday. Otherwise it will output "Have a nice day, my friends!":


(click image to view)