本机PHP函数中的异常

Cai*_*aio 0 php native exception function

PHP中的大多数函数返回true/false:

var_dump (is_int ("1")); // false
Run Code Online (Sandbox Code Playgroud)

我可以配置PHP来返回异常而不是布尔值吗?

try {is_int ("1")} catch (Exception $e) {exit ($e->getMessage ());}
Run Code Online (Sandbox Code Playgroud)

谢谢.

Urd*_*rda 5

难道你不能用一次投掷吗?

<?php

function myFunction($var)
{    
    if(!(is_int($var))
    {
        throw new Exception('Custom message about the error');
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

只是有一个try/catch块来捕捉你的问题?

<?php
try
{
    myFunction(1);
    myFunction("1");
}
catch
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}
?>
Run Code Online (Sandbox Code Playgroud)