Page 1 of 1

throwing exception

Posted: Sun Oct 15, 2006 1:03 pm
by iffo
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

Can someone explain me what is throwing exception is ?, how it works. Someone gave me this example , but I don  not undersnatd , if you have any code where you actually use it , that will be really helpful

Example

Code: Select all

<?php
class MyException {
   function __construct($exception) {
       $this->exception = $exception;
   }

   function Display() {
       print "MyException: $this->exception\n";
   }
}

class MyExceptionFoo extends MyException {
   function __construct($exception) {
       $this->exception = $exception;
   }

   function Display() {
       print "MyException: $this->exception\n";
   }
}

try {
   throw new MyExceptionFoo('Hello');
}
catch (MyException $exception) {
   $exception->Display();
}
catch (Exception $exception) {
   echo $exception;
}
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Oct 15, 2006 1:08 pm
by feyd
Exceptions are a programmatic way of consolidating (programmer created) error handling. In the example you've been provided an exception is simply artificially thrown, so it's not the best example of common usage, but it is the general idea behind it.

viewtopic.php?t=50490 may be of interest.