dongshi6710 2016-10-16 04:21
浏览 50
已采纳

不在对象方法内捕获异常

This is more just for documentation, since I've already solved the issue, but it was subtle and difficult enough to debug that I thought it would be useful in the public sphere.

The issue was that I had a try/catch block in an object method that just wasn't working. The reduced example is in two files, which look like this:

TestClass.php:

<?php
//TestClass.php
namespace MyTest;

class TestClass {
    public function __construct() {
        \e("Initializing object");
        try {
            \e("Trying object exception");
            \throwTestException("Failing gracefully in object");
            \e("After exception");
        } catch (Exception $e) {
            \e($e->getMessage());
        }
        \e("After object init exception");
    }
}
?>

Main.php:

<?php
//Main.php

function e($str) { echo "
$str"; }
function throwTestException($msg) {
    throw new RuntimeException($msg);
}
require "TestClass.php";

e("Beginning");
try {
    e("First try");
    throwTestException("Failing gracefully first");
    e("After exception");
} catch (Exception $e) {
    e($e->getMessage());
}
e("Ending");

e('');

e('Beginning object test');
new \MyTest\TestClass();
e('Ending object test');
?>

The expected result on loading Main.php was this:

Beginning
First try
Failing gracefully first
Ending

Beginning object test
Initializing object
Trying object exception
Failing gracefully in object
After object init exception
Ending object test

What I actually got was something like this:

Beginning
First try
Failing gracefully first
Ending

Beginning object test
Initializing object
Trying object exception

Fatal Error: Uncaught Exception: Failing gracefully in object......

As you can see, the exception was not being caught. I tried all sorts of things and just couldn't figure out why it wasn't being caught. And then.... (See answer below)

  • 写回答

1条回答 默认 最新

  • douyunhuan9886 2016-10-16 04:21
    关注

    I realized it was a namespace issue. Because I had declared TestClass within the namespace MyTest, and throwTestException in the global namespace, my reference to Exception within the class method was tacitly resolving to \MyTest\Exception and thus NOT matching the actual exception being thrown, \RuntimeException. And since I wasn't actually trying to instantiate the exception from within the namespace, no "Unknown Class" errors emerged to reveal what was happening.

    The solution, then, was simply to properly resolve the exception class I was trying to catch:

    catch(\Exception $e) { .... }
    

    To be fair, this became obvious as I built my highly reduced example. It wasn't obvious initially because the exception I was expecting to catch was being generated by the class's superclass (which was the SQLite3 class). Thus, I didn't have to worry about the namespace when generating the exception, and all I was thinking about when catching it was to use the most general form of exception, Exception. And again, since I wasn't instantiating that exception -- only matching against it in a catch block --, I didn't get any notices that it was unresolved.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)