Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Exception

Represent an exceptional application condition with a message, integer code and optional previous Throwable.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

new Exception($message, $code, $previous)

Represent an exceptional application condition with a message, integer code and optional previous Throwable. Throwing interrupts normal flow until a matching catch. When translating a failure, pass the original as previous to retain the cause. Catching Exception alone does not catch engine Error objects. The Exception::getMessage method returns the message of that particular exception, here "job". Read the cause’s message "source" separately through $e->getPrevious()?->getMessage() if a previous exception exists.

$cause=new RuntimeException("source",8);
try { throw new Exception("job",12,$cause); }
catch(Exception $e){echo json_encode([$e->getMessage(),$e->getCode(),$e->getPrevious()->getMessage()]);}
["job",12,"source"]

Examples

<?php
$cause=new RuntimeException("source",8);
try { throw new Exception("job",12,$cause); }
catch(Exception $e){echo json_encode([$e->getMessage(),$e->getCode(),$e->getPrevious()->getMessage()]);}

Result

["job",12,"source"]

Sources