46

Can I declare a function in php that throws an exception? For example:

public function read($b, $off, $len) throws IOException 
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
shay
  • 1,317
  • 4
  • 23
  • 35

3 Answers3

52

You can use @throws in the PHPDoc comment, and the IDE will recognize this function as throwing an exception, when viewing the doc, however unlike Java it will not force you to implement the Try{}catch block. Maybe future versions of the IDE (I am using InteliJ 11) will mark those places where try{}catch is expected, that same as it already do with the JavaScript types marked by doc (e.g. String}) when recognizing inconsistency.

In short, using Doclet like when codding with scripting languages (PHP, JavaScript..), is turn to be complementary tool for safer programming in the case of non type-safe and non compiled languages.

like this:

enter code here
/**
 * Handle 'get' operations
 * @abstract
 * @param int $status reference for setting the response status
 * @param String $body reference for setting the response data
 * @return mixed
 * @throws Exception if operation fail
 */
function get(&$status, &$body) {
}

enter image description here

AleX
  • 399
  • 3
  • 10
Gilad
  • 729
  • 1
  • 6
  • 5
19

I misread the question, see the answer below from Gilad (which should be accepted).

Previous answer:

You could throw a new exception from the body of the function. It's all described here

Example:

<?php
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    else return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

// Continue execution
echo 'Hello World';
?>
Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
  • yes i read it , by i was thinking maybe it posible and i missed the tutorial . ok so i cant declare a method that throws an exception ? what about IOException there is such thing o i need to create it my self? – shay Jul 24 '10 at 13:42
  • No, it doesn't exist already. But you can declare it (extend an appropriate SPL exception ( `class IOException extends RuntimeException {}`) – ircmaxell Jul 24 '10 at 13:55
  • 2
    Correct answer is by Gilad. You need to use PHPDoc. – Tomas Bruckner May 04 '18 at 09:54
  • 3
    This is not the answer to this question. The question was how to **declare**, not how to throw an exception. – AleX Oct 10 '18 at 11:08
6

For a list of exceptions that come with the SPL: SPL Exceptions.

If you want to create your own exception:

From the PHP Exceptions page:

The thrown object must be an instance of the Exception Class or a subclass of Exception. Trying to throw an object that is not will result in a PHP Fatal Error.

So yes, it is possible to create your own exceptions. Just a bit of reading will help you achieve what you want.

Jim
  • 18,673
  • 5
  • 49
  • 65
  • thank you what about declaring the function as throwing an exception? i just need to throw the exception – shay Jul 24 '10 at 13:55
  • Do i must catch every exception , also if it a simple exception like OutOfRangeException? cant i run code without catching any exception ? – shay Jul 24 '10 at 14:02