Php throw new exception parameters

What I do, is have a class to create custom exceptions, but to standardise matters, I simply have one additional argument, which is an object [well, so far it's always been an array], which allows me to specify unlimited amount of exception data [much like a javascript exception].

Output:

Fatal error: Uncaught SqlProxyException 'Duplicate entry '1' for key 'PRIMARY'' in     /usr/src/wrangler/trunk/common/SqlProxy.php[356]
#0 /usr/src/wrangler/trunk/common/SqlProxy.php[341]: SqlProxy::Update['INSERT into tes...']
#1 /usr/src/wrangler/trunk/common/SqlProxy.php[455]: SqlProxy::Insert['INSERT into tes...']
#2 {main}
Array
[
[sql] => INSERT into test SET test='1'
[errorObject] => Array
    [
        [status] => UNKNOWN
        [data] => Array
            [
                [rowsAffected] => -1
                [errorMsg] => Duplicate entry '1' for key 'PRIMARY'
                [errorCode] => 1062
....

This is achieved by the following in my code:



ask at nilpo dot com

13 years ago

If you intend on creating a lot of custom exceptions, you may find this code useful.  I've created an interface and an abstract exception class that ensures that all parts of the built-in Exception class are preserved in child classes.  It also properly pushes all information back to the parent constructor ensuring that nothing is lost.  This allows you to quickly create new exceptions on the fly.  It also overrides the default __toString method with a more thorough one.

Shot [Piotr Szotkowski]

13 years ago

‘Normal execution [when no exception is thrown within the try block, *or when a catch matching the thrown exception’s class is not present*] will continue after that last catch block defined in sequence.’

‘If an exception is not caught, a PHP Fatal Error will be issued with an “Uncaught Exception …” message, unless a handler has been defined with set_exception_handler[].’

These two sentences seem a bit contradicting about what happens ‘when a catch matching the thrown exception’s class is not present’ [and the second sentence is actually correct].

Edu

9 years ago

The "finally" block can change the exception that has been throw by the catch block.



The output is:

Hello catch in
Hello finally
Bye catch out

Simo

7 years ago

#3 is not a good example. inverse["0a"] would not be caught since [bool] "0a" returns true, yet 1/"0a" casts the string to integer zero and attempts to perform the calculation.

daviddlowe dot flimm at gmail dot com

4 years ago

Starting in PHP 7, the classes Exception and Error both implement the Throwable interface. This means, if you want to catch both Error instances and Exception instances, you should catch Throwable objects, like this:

mlaopane at gmail dot com

4 years ago



Tom Polomsk

7 years ago

Contrary to the documentation it is possible in PHP 5.5 and higher use only try-finally blocks without any catch block.

Sawsan

10 years ago

the following is an example of a re-thrown exception and the using of getPrevious function:

Daan

1 year ago

I would like to emphasise that you can not rethrow an Exception inside a catch-block and expect that the next catch-block will handle it.

How do you throw a new exception?

Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception includes a message which is a human-readable error description. It can often be related to problems with user input, server, backend, etc.

Does PHP have try catch?

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

What is an exception how you can handle multiple exceptions in PHP?

Rules for exceptions.
Code may be surrounded in a try block, to help catch potential exceptions..
Each try block or "throw" must have at least one corresponding catch block..
Multiple catch blocks can be used to catch different classes of exceptions..
Exceptions can be thrown [or re-thrown] in a catch block within a try block..

How do you extend an exception class?

The class extends the Exception class that is defined in the Java core API [in the package is java. lang ]. When extending Exception you are defining a "checked" exception, i.e., an exception that must be caught or thrown. A constructor is provided that passes the message argument to the super class Exception .

Chủ Đề