Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to throw and catch exceptions with try - catch in PHP

1 Answer

0 votes
function div($a, $b) 
{
    if ($b == 0) 
        throw new Exception('Division by zero');
    return $a/$b;
}

try 
{
    echo div(10, 2) . "<br />";
    echo div(22, 0) . "<br />";
    echo div(60, 3) . "<br />";
} 
catch (Exception $e) 
{
    echo 'Exception: ' .  $e->getMessage() . '<br />';
}
 
 
echo "Countinue with the program"; 
 
/*
run:
 
5
Exception: Division by zero
Countinue with the program
 
*/

 



answered Jul 24, 2015 by avibootz
edited Jul 24, 2015 by avibootz

Related questions

3 answers 298 views
298 views asked Jan 3, 2016 by avibootz
2 answers 269 views
2 answers 268 views
1 answer 189 views
1 answer 168 views
168 views asked Dec 17, 2016 by avibootz
2 answers 334 views
2 answers 401 views
...