2
respostas

Dúvida sobre Exception no PHP

  //creating a function containing a potential exception
  function checkNumber($number) {
    if($number > 1) {
      throw new Exception('The value has to be 1 or lower');
    }
    return true;
  }

  //triggering the exception inside a try()
  try {
    checkNumber(2);
    //In case the exception gets thrown, this is the message that is shown.
    echo 'The value is 1 or lower';
  } catch(Exception $e) {
    //catching the exception
    echo 'Message: ' .$e->getMessage();
  }

Alguém sabe dizer se há alguma diferença se colocarmos a declaração dessa função dentro do bloco "try"?

2 respostas

Olá, Daniel.

Não faz muito sentido adicionar definições dentro de blocos try-catch...

Por que você iria fazer isso?

Olá, Daniel. Você continua com dúvida nesse assunto?

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software