Does an exception in PHP destroy the stack trace again?

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

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

    //Re-throw
    throw e;
}

Because of this, it is preferable to use throw, rather than throw e. This will allow you to propagate the same exception up, instead of wrapping it in a new one.

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

<?php

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

    //Re-throw
    throw $e;
}
+4
source share
3 answers

$e PHP, , , , stacktrace .

, :

throw new RuntimeException( $e->getMessage() );
+3

. , . , , .

, , .

, .

0

Throwing the same exception again will not destroy the stack trace. But depending on what you need, you can just throw the same exception or create an exception chain (see PHP Documentation> Exception :: __ construct )

A very good explanation of when and why you can choose one approach over another is given in this answer.

0
source

Source: https://habr.com/ru/post/1625319/


All Articles