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.

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

报告相同问题?