dpkt17803 2019-02-02 10:04
浏览 636

为什么不回显$ e-> getMessage(); 打印错误消息

I cant get this code to print the error message. I'm fairly new to php so go easy on me aha. I'm trying to make a register page for my website, feel free to request more of the code if it's required to help me.

try {
    //Username
    if(strlen($username) < 4) {
        throw new Exception('Username must be at least 4 Characters!');
    }
    if(strlen($username) > 16){
        throw new Exception('Username cannot be more than 15 characters!');
    }


    if(!ctype_alnum($username)) {
        throw new Exception('Username must only be letters or numbers');
    }

    // Password
    if(strlen($password) < 8) {
        throw new Exception('Password must be at least 8 characters!'); 
    }

    // email
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
        throw new Exception('Invalid Email!'); 
    }

    // secretcode
    if(strlen($secretcode) < 6) {
        throw new Exception('Secret Code is too short!'); 
    }

    if(strlen($secretcode) > 10) {
        throw new Exception('Secret Code is too long!'); 
    }

    //repassword
    if(strlen($password!=$repassword)) {
        throw new Exception('Passwords did not match');
    }


    // Submit to database
    echo 'ok';

} catch (Exception $e) {
    echo $e->getMessage();
}

Can't get echo to print out error message

  • 写回答

1条回答 默认 最新

  • dtnpzghys01643322 2019-02-02 17:28
    关注

    This is a common issue when working with classes. Just add this one line at the very top but after namespace declaration

    namespace My\Workspace;
    use Exception; //<-- this one
    
    class Myclass 
    {
        try {
            //your code
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    
    }
    

    This is because if you don't use use php will go looking in My\Workspace\Exception which obviously doesn't exists and therefore the catch will never run.

    If you don't wish to add the use Exception; declaration, a simple shortcut would be to add a backward slash in front of Exception

    catch (\Exception $e)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM