【问题标题】:PHP - Don't load the page if exception is caughtPHP - 如果捕获到异常,则不要加载页面
【发布时间】:2017-01-22 20:58:37
【问题描述】:

我是 OO PHP 新手...我正在尝试创建一个名为 MyClass 的 PHP 类和一些应该:

  1. 验证参数是否为数字
  2. 验证参数是否已定义
  3. 如果上述任何一项失败,我需要发送异常并附上解释

当我像这样调用MyFunction 方法时:

$quotation = new Quotations\MyClass(); 
echo $quotationResult = $quotation -> MyFunction('A', 'B', 2, 'a'); 

测试它是否会抛出异常,但由于某种原因页面没有加载说 - “本地主机当前无法处理此请求”。但是当我这样称呼它时:

$quotation = new Quotations\MyClass(); 
echo $quotationResult = $quotation -> MyFunction('A', 'B', 2, 3); 

它工作正常(我可以看到空白页)。

为什么它不允许我在浏览器中打印异常?

我的脚本:

<?php

namespace Quotations;

class MyClass {

var $is_number;
var $is_defined;
private $number;
private $defined;

private function isNumeric($w, $c){
    if(is_numeric($w) && is_numeric($c)){
        $number = true;
    }
    else{
        $number = false;
    }

    return $number;
}

private function isDefined($t, $f, $w, $c){
    if(isset($t, $f, $w, $c)){
        $defined = true;
    }
    else{
        $defined = false;
    }

    return $defined;
}

function MyFunction($to, $from, $weight, $cube) {
    try{
        if(!$this -> isNumeric($weight, $cube)){
            throw new InvalidArgumentException('Arguments are not numeric');
        }

        if(!$this -> isDefined($to, $from, $weight, $cube)){
            throw new BadMethodCallException('Arguments are missing');
        }
    }catch(InvalidArgumentException $e){
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }catch(BadMethodCallException $e){
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    } 
}
 }
?>

【问题讨论】:

  • 您缺少 MyClass 声明的右大括号,因此它应该始终作为语法错误而失败。启用错误报告以确保您看到 PHP 的错误输出
  • 谢谢,我已经添加了大括号,但输出仍然相同!
  • 不要将解决方案编辑到问题中。将其发布为答案。这样,人们就可以看到最初的问题是什么,以及它与解决方案有何不同。
  • @Barmar,如果您愿意,可以发布正确答案,我会将其标记为已解决!

标签: php class exception methods


【解决方案1】:

在执行名称前使用反斜杠以在全局命名空间中查找它们。否则,它会在 Quotations 命名空间中查找它们。

function MyFunction($to, $from, $weight, $cube) {
    try {
        if(!$this -> isNumeric($weight, $cube)){
            throw new \InvalidArgumentException('Arguments are not numeric');
        }

        if(!$this -> isDefined($to, $from, $weight, $cube)){
            throw new \BadMethodCallException('Arguments are missing');
        }
    } catch(\InvalidArgumentException $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    } catch(\BadMethodCallException $e) {
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    } 
}

【讨论】:

    【解决方案2】:

    第一步是启用错误报告 -
    error_reporting(E_ALL);
    ini_set("display_errors", 1);.

    通过查看您的代码,您似乎缺少一个右括号,您的代码应该如下所示。

    namespace Quotations;
    
    class MyClass {
        var $is_number;
        var $is_defined;
        private $number;
        private $defined;
    
        private function isNumeric($w, $c) {
            if(is_numeric($w) && is_numeric($c)){
                $number = true;
            }
            else {
                $number = false;
            }
    
            return $number;
        }
    
        private function isDefined($t, $f, $w, $c){
            if( isset( $t, $f, $w, $c ) ){
                $defined = true;
            }
            else {
                $defined = false;
            }
    
            return $defined;
        }
    
        function MyFunction($to, $from, $weight, $cube) {
            try {
                if(!$this -> isNumeric($weight, $cube)){
                    throw new InvalidArgumentException('Arguments are not numeric');
                }
    
                if(!$this -> isDefined($to, $from, $weight, $cube)){
                    throw new BadMethodCallException('Arguments are missing');
                }
            } catch(InvalidArgumentException $e) {
                echo 'Caught exception: ',  $e->getMessage(), "\n";
            } catch(BadMethodCallException $e) {
                echo 'Caught exception: ',  $e->getMessage(), "\n";
            } 
        }
    }
    

    编辑:

    我看到你在 cmets 中说你已经添加了} 并且仍然没有运气所以我查看了你的代码并发现了以下问题

    所以这是我能想到的最后一个解决方案 xD

    function MyFunction( $to, $from, $weight, $cube ) {
        try
        {
            try
            {
                if( !$this->isNumeric( $weight, $cube ) ) {
                    throw new \InvalidArgumentException( 'Arguments are not numeric' );
                }
            } catch ( \InvalidArgumentException $e ) {
                echo 'Error: ' . $e->getMessage();
            }
            try
            {
                if( !$this->isDefined( $to, $from, $weight, $cube ) ){
                    throw new \BadMethodCallException( 'Arguments are missing' );
                }
            } catch ( \BadMethodCallException $e ) {
                echo 'Error: ' . $e->getMessage();
            }
        } catch ( Exception $e ) {
            echo 'Error: ' . $e->getMessage();
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 这种错误我遇到了 - 致命错误:找不到 Class Quotations\BadMethodCallException
    • 尝试从您的代码中删除isDefined,然后重试
    • @DenissMuntjans 尝试使用\BadMethodCallException,这样它就会在全局命名空间中查找。
    • 把反斜杠也放在catch() 子句中。
    • 更改if 测试的顺序,以便首先检查缺少的参数。
    猜你喜欢
    • 2019-03-13
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    相关资源
    最近更新 更多