Does re-throwing an exception in PHP destroy the stack trace?
Re-throwing the same exception will not destroy the stack trace. But depending on what you need, you might want to just throw the same exception or build an Exception Chaining ( see PHP Documentation > Exception::__construct )
A very good explanation of when and why one would choose one approach over another is given in this answer
When you throw $e in PHP like you did you rethrow the exisiting exception object without changing anything of its contents and send all given information including the stacktrace of the catched exception - so your second example is the correct way to rethrow an exception in PHP.
If (for whatever reason) you want to throw the new position with the last message, you have to rethrow a newly created exception object:
throw new RuntimeException( $e->getMessage() );
Note that this will not only lose the stack trace, but also all other information which may be contained in the exception object except for the message (e.g. Code
, File
and Line
for RuntimeException
). So this is generally not recommended!