Can I declare that a php function throws an exception?

36,615

Solution 1

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';
?>

Solution 2

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

Solution 3

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.

Share:
36,615
shay
Author by

shay

Updated on July 09, 2022

Comments

  • shay
    shay almost 2 years

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

    public function read($b, $off, $len) throws IOException 
    
  • shay
    shay almost 14 years
    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
    shay almost 14 years
    thank you what about declaring the function as throwing an exception? i just need to throw the exception
  • ircmaxell
    ircmaxell almost 14 years
    No, it doesn't exist already. But you can declare it (extend an appropriate SPL exception ( class IOException extends RuntimeException {})
  • shay
    shay almost 14 years
    Do i must catch every exception , also if it a simple exception like OutOfRangeException? cant i run code without catching any exception ?
  • wtf8_decode
    wtf8_decode over 8 years
    This should IMO be the accepted answer (OP wanted to make it explicit that a function could throw an exception and was not asking how to throw one), though the massive trailing blank space in the image should be removed.
  • Tomas Bruckner
    Tomas Bruckner almost 6 years
    Correct answer is by Gilad. You need to use PHPDoc.
  • AleX
    AleX over 5 years
    This is not the answer to this question. The question was how to declare, not how to throw an exception.
  • Toskan
    Toskan over 5 years
    what about php 7? still nothing?
  • Rich Court
    Rich Court almost 5 years
    Nope, still nothing.