Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

577
Visualizações
Please what does (PDOException $e) and PDOException($e->getMessage(), (int)$e->getCode()) mean in simplest terms?

Can you please explain what (PDOException $e) and PDOException($e->getMessage(), (int)$e->getCode()) does and mean here?

try
  {
    $pdo = new PDO($attr, $user, $pass, $opts);
  }
  catch (PDOException $e)
  {
    throw new PDOException($e->getMessage(), (int)$e->getCode());
  }
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The best place to start is the PHP manual page on Exceptions.

Exceptions are a mechanism of mandatory error handling - rather than setting a flag that subsequent code must actively check, throwing an exception aborts all running code until it reaches a matching catch block. If no catch block is found, the entire script exits.

catch blocks match using the same logic as the instanceof operator - you name a class or interface, and the exception must be the same class, a sub-class, or implement that interface. You then give a variable name - in your example $e, but it can be any name you like - and PHP assigns the exception to that variable and runs the content of the catch block.

Creating an exception is the same as creating any other object - you use the new operator. Once created, they can have properties and methods, including a default set which includes getMessage() and getCode(), which return values which were passed when originally constructed. Their special behaviour happens when you use the throw keyword.

So, the code you've shown:

  1. Runs some code that might throw an exception (new PDO(...))
  2. Catches any exception matching instanceof PDOException
  3. Looks up the "message" and "code" on that exception object
  4. Creates a new PDOException object
  5. Throws that new exception

The result on its own is pretty much useless - in particular, steps 4 and 5 can be replaced by throw $e, which essentially means "I've looked at this exception, but decided it's still a problem; carry on looking for another matching catch block". That's occasionally useful if you want to do something between steps 3 and 4, such as logging or cleanup.

As pointed out in another answer it does remove the backtrace, which might otherwise expose credentials. That would be better handled by setting zend.exception_ignore_args=true in your PHP configuration. PHP 8.2 will also have a new facility that automatically hides certain parameters in backtraces.

over 4 years ago · Santiago Trujillo Relatório

0

Let's compare the output of this code with and without try catch

$pdo = new PDO('foo', 'username', 'password');

will give us

PHP Fatal error: Uncaught PDOException: PDO::__construct():
Argument #1 ($dsn) must be a valid data source name in pdo.php:6
Stack trace:
#0 C:\SERVER\www\htdocs\pdo.php(6): PDO->__construct('foo', 'username', 'password')
#1 {main} thrown in pdo.php on line 6

notice the username and password can be seen in the error message

Now with try catch and re-throw

try
{
    $pdo = new PDO('foo', 'username', 'password');
}
catch (PDOException $e)
{
    throw new PDOException($e->getMessage(), (int)$e->getCode());
}

the output will be

PHP Fatal error:  Uncaught PDOException: PDO::__construct(): 
Argument #1 ($dsn) must be a valid data source name in pdo.php:12
Stack trace:
#0 {main}
  thrown in pdo.php on line 12

as you can see, there is no username or password.

This is the only reason such a clumsy code is employed

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda