Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Exception in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 106
    Comment on it

    Welcome to FindNerd. We are going to discuss the Exception,what is it and how we can use it in php? First question araised that what is Exception? Exception is nothing but a runtime error. It depends totally upon the values passing at run time. We can take an example of division. In division we try to divide any number with zero then we need to throw the Exception division by zero. Please have a look.

    <?php
    function divisionOpt($num1,$num2)
    {
    if(!$num2 || $num2==0)
    {
    throw new Exception('Division by zero');
    }
    return $num1/$num2;
    }
    try
    {
    echo divisionOpt(55,6);
    echo divisionOpt(44,0);
    }
    catch(Exception $e)
    {
    echo 'Exception:' . $e->getMessage();
    }
    ?>
    result: 9.1666
    Exception: Division by zero
    

    In above example we build an function for division and called this function twice with different arguments. In second call, we passes zero as second argument. It will throw the exception Division by zero.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: