0

I've got a (example) Oracle Stored Procedure:

CREATE OR REPLACE FUNCTION EXCEPTION_TEST
  RETURN NUMBER
AS 
BEGIN
  raise_application_error(-20500, 'This is the exception text I want to print.');
END;

and I call it in PHP with PDO with the following code:

$statement = $conn->prepare('SELECT exception_test() FROM dual');
$statement->execute();

The call of the function works perfectly fine, but now I want to print the Exception text only.

I read somewhere, that you should not use try and catch with PDO. How can I do this?

Stjubit
  • 37
  • 1
  • 8

2 Answers2

1

You have read that you shouldn't catch an error to report it.

However, if you want to handle it somehow, it's all right to catch it.

Based on the example from my article on handling exception in PDO,

try {
    $statement = $conn->prepare('SELECT exception_test() FROM dual');
    $statement->execute();
} catch (PDOException $e) {
    if ($e->getCode() == 20500 ) {
        echo $e->getmessage();
    } else {
        throw $e;
    }
}

Here you are either getting your particular error or re-throwing the exception back to make it handled the usual way

Your Common Sense
  • 152,517
  • 33
  • 193
  • 313
  • Okay, thank you. I'll do it in this way now. However, it's not possible to get the exception text only, or is it? – Stjubit Jan 22 '17 at 11:41
  • I don't understand your question. you want this text as a function's output? – Your Common Sense Jan 22 '17 at 12:12
  • Yeah. I want to output the exception text ('This is the exception text I want to print.') to the user, because I'm doing error detection in the SQL Stored Procedures and Functions. The 'getMessage' method prints a whole lot of stuff, but I only want to get the exception text. Is splitting the message to get only the exception text the only way to achieve this? – Stjubit Jan 22 '17 at 18:55
  • I am afraid yes, – Your Common Sense Jan 22 '17 at 19:41
0

You check the execute response and get the error, for example, like this:

if ($statement->execute() != true) {
    echo $statement->errorCode();
    echo $statement->errorInfo();
}

You can find more options at the PDO manual.

Peon
  • 7,216
  • 7
  • 45
  • 87
  • Okay, thank you. That works. Now I get the following output: `Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 20500 OCIStmtExecute: ORA-20500: This is the exception text I want to print. ORA-06512: at "EPCOS.EXCEPTION_TEST", line 5 (ext\pdo_oci\oci_statement.c:148)' in C:\xampp\htdocs\epcos\test.php:9 Stack trace: #0 C:\xampp\htdocs\epcos\test.php(9): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\epcos\test.php on line 9` How can I only print the exception text (This is the exception...)? – Stjubit Jan 22 '17 at 10:36
  • Well, if you want to handle the specific exceptions, then you use `try/catch` (http://stackoverflow.com/questions/11102644/pdo-exception-questions-how-to-catch-them). If you want to know, if the result was a success, then you use a simple `if`. – Peon Jan 22 '17 at 10:41
  • So, you mean that it's not possible to get the exception text and I have to check if the exception has the code (-20500) and print the text with PHP? – Stjubit Jan 22 '17 at 10:46