Does re-throwing an exception in PHP destroy the stack trace?

Hugo Zink

In C#, doing the following would destroy the stack trace of an exception:

try{
    throw new RuntimeException();
}
catch(Exception e){
    //Log error

    //Re-throw
    throw e;
}

Because of this, using throw rather than throw e is preferred. This will let the same exception propagate upwards, instead of wrapping it in a new one.

However, using throw; without specifying the exception object is invalid syntax in PHP. Does this problem simply not exist in PHP? Will using throw $e as follows not destroy the stack trace?

<?php

try{
    throw new RuntimeException();
}
catch(Exception $e){
    //Log error

    //Re-throw
    throw $e;
}
user5815430

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Throwing an exception more than once loses its original stack trace

From Dev

How does the console obtain the stack trace of an exception?

From Dev

Why does android logcat not show the stack trace for a runtime exception?

From Dev

Exception Stack Trace Missing Items

From Dev

Why is the exception stack trace not logged in?

From Dev

Node not throwing Stack Overflow exception

From Dev

re throwing const exception reference

From Java

How to print the stack trace of an exception object in Python?

From Dev

Exception that emails stack trace when raised

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

Rethrow an Exception with correct line numbers in the stack trace

From Dev

How can an Exception be created/thrown with no stack trace?

From Dev

logback: newline before exception stack trace but not otherwise?

From Dev

Exception stack trace and aggregation in Parallel.ForEach

From Dev

Suppressing stack trace when catching exception

From Dev

How to print the stack trace of an exception object in Python?

From Dev

locals() and globals() in stack trace on exception (Python)

From Dev

How to use a stack trace dump to debug an exception?

From Dev

cxf null pointer exception without stack trace

From Dev

Printing the stack trace vs the exception itself

From Dev

C++ Exception re-throwing and elipsis

From Java

Get exception description and stack trace which caused an exception, all as a string

From Dev

Behat with PhpStorm does not show stack trace for exceptions

From Dev

What does "eliminated" mean in a Java stack trace?

From Dev

What does a slash mean in a Java stack trace?

From Dev

Show Full Stack Trace on PHP Command Line

From Dev

Stack Trace of HTML page rendered in cake php

From Dev

does throwing Exception kills thread in node js?

Related Related

  1. 1

    Throwing an exception more than once loses its original stack trace

  2. 2

    How does the console obtain the stack trace of an exception?

  3. 3

    Why does android logcat not show the stack trace for a runtime exception?

  4. 4

    Exception Stack Trace Missing Items

  5. 5

    Why is the exception stack trace not logged in?

  6. 6

    Node not throwing Stack Overflow exception

  7. 7

    re throwing const exception reference

  8. 8

    How to print the stack trace of an exception object in Python?

  9. 9

    Exception that emails stack trace when raised

  10. 10

    locals() and globals() in stack trace on exception (Python)

  11. 11

    How to use a stack trace dump to debug an exception?

  12. 12

    Rethrow an Exception with correct line numbers in the stack trace

  13. 13

    How can an Exception be created/thrown with no stack trace?

  14. 14

    logback: newline before exception stack trace but not otherwise?

  15. 15

    Exception stack trace and aggregation in Parallel.ForEach

  16. 16

    Suppressing stack trace when catching exception

  17. 17

    How to print the stack trace of an exception object in Python?

  18. 18

    locals() and globals() in stack trace on exception (Python)

  19. 19

    How to use a stack trace dump to debug an exception?

  20. 20

    cxf null pointer exception without stack trace

  21. 21

    Printing the stack trace vs the exception itself

  22. 22

    C++ Exception re-throwing and elipsis

  23. 23

    Get exception description and stack trace which caused an exception, all as a string

  24. 24

    Behat with PhpStorm does not show stack trace for exceptions

  25. 25

    What does "eliminated" mean in a Java stack trace?

  26. 26

    What does a slash mean in a Java stack trace?

  27. 27

    Show Full Stack Trace on PHP Command Line

  28. 28

    Stack Trace of HTML page rendered in cake php

  29. 29

    does throwing Exception kills thread in node js?

HotTag

Archive