diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-06 16:24:19 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2014-03-06 16:24:19 +0100 |
commit | 62f743b7f7dff333d999f3070a52f2874dcd9488 (patch) | |
tree | b7c4a98e53465a44f9623d41c9b78322b5d2e0ec /documentation/exceptions.html | |
parent | dfd505867630f0d8e7c45f52415f8aba3b3c2dba (diff) |
changes to documentation
Diffstat (limited to 'documentation/exceptions.html')
-rw-r--r-- | documentation/exceptions.html | 58 |
1 files changed, 57 insertions, 1 deletions
diff --git a/documentation/exceptions.html b/documentation/exceptions.html index 6f65105..485f0de 100644 --- a/documentation/exceptions.html +++ b/documentation/exceptions.html @@ -1 +1,57 @@ -<h1>Exceptions</h1>
\ No newline at end of file +<h1>Exceptions</h1> +<p> + PHP and C++ both support exceptions, and with the PHP-CPP library exception + handling between these two languages has become completely transparent - + which could very well be the coolest feature of the PHP-CPP library. + Exceptions that you throw in C++ are automatically passed on to the PHP + script, and exceptions thrown by PHP scripts can be caught by your C++ + code as if it was a plain C++ exception. +</p> +<p> + Let's start with a simple C++ function that throws an exception. +</p> +<p> +<pre class="language-c++"><code>#include <phpcpp.h> + +Php::Value myDiv(Php::Parameters ¶ms) +{ + // division by zero is not permitted, throw an exception when this happens + if (params[1] == 0) throw Php::Exception("Division by zero"); + + // divide the two parameters + return params[0] / params[1]; +} + +extern "C" { + PHPCPP_EXPORT void *get_module() { + static Php::Extension extension("my_extension", "1.0"); + extension.add("myDiv", myDiv, { + Php::ByVal("a", Php::Type::Numeric, true), + Php::ByVal("b", Php::Type::Numeric, true) + }); + return extension; + } +}</code></pre> +</p> +<p> + And once again you see a very simple extension. In this extension a "myDiv" + function is created that divides two numbers. But division by zero is of + course not allowed, so when an attempt is made to divide by zero, an + exception is thrown. The following PHP script uses this: +</p> +<p> +<pre class="language-php"><code> +<?php +try +{ + echo(myDiv(10,2)."\n"); + echo(myDiv(8,4)."\n"); + echo(myDiv(5,0)."\n"); +} +catch (Exception $exception) +{ + echo($exception); +} +> +</code></pre> +</p>
\ No newline at end of file |