Answers for "php throw exception"

PHP
6

php throw exception

function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}
Posted by: Guest on February-27-2020
1

try catch php

function inverso($x) {
    if (!$x) {
        throw new Exception('Zero division.');
    }
    return 1/$x;
}

try {
    echo inverso(5) . "\n";
    echo inverso(0) . "\n";
} catch (Exception $e) {
    echo 'and the error is: ',  $e->getMessage(), "\n";
}
Posted by: Guest on December-13-2020
0

php catch exception

<?php
function inverse($x) {
    if (!$x) {
       throw new Exception('Division durch Null.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
    echo inverse(0) . "\n";
} catch (Exception $e) {
    echo 'Exception abgefangen: ',  $e->getMessage(), "\n";
}

// Ausführung fortsetzen
echo "Hallo Welt\n";
?>
Posted by: Guest on August-03-2020
2

try catch in php

Try catch comes under exception handeling concept where using this we control the runtime error and modify the message as we want.

// function created with exception throw
function checkdata($number){
	if($number > 10){
    	throw new Exception("Number is greater than 10");
    }
    return true;
}

// try block starts
try{
	checkdata(15);
  	echo "The number is below 10";
}
// catch block
catch(Exception $e){
	echo "Message :".$e->getMessage();
}

In above code if condition is not satisfied then it will throw exception and which gets caught by catch block and show the error message.
Posted by: Guest on April-16-2020
0

php throw fatal error

trigger_error("Fatal error", E_USER_ERROR);
Posted by: Guest on September-09-2021
-2

php multiple catch exceptions

try
{
    // Some code...
}
catch(AError | BError $e)
{
    // Handle exceptions
}
catch(Exception $e)
{
    // Handle the general case
}
Posted by: Guest on April-22-2020

Browse Popular Code Answers by Language