Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP尝试捕捉一些问题_Php_Try Catch - Fatal编程技术网

PHP尝试捕捉一些问题

PHP尝试捕捉一些问题,php,try-catch,Php,Try Catch,你好。是否可以在PHP中使用此类代码 try { throw new InternalException('Internal'); } catch (InternalException $e) { throw new Exception('Internal To global'); } catch (Exception $e){ print $e->getMessage(); } class InternalException extends Exception { //

你好。是否可以在PHP中使用此类代码

try {
  throw new InternalException('Internal');
} catch (InternalException $e) {
  throw new Exception('Internal To global');
} catch (Exception $e){
  print $e->getMessage();
}

class InternalException extends Exception {
  // some code here
}

是的,您可以单独捕获特定异常。

只有在
try
块中抛出异常时,才会捕获异常。
catch
块中抛出的异常不会在同一
try..catch
语句的其他同级
catch
块中捕获。您必须将整个内容嵌套在另一个外部
try..catch
块中才能捕获这些内容

Nest multiple
try…catch

try {
    throw new InternalException('Internal');
} catch (InternalException $e) {
    try {
        throw new Exception('Internal To global');
    } catch (Exception $e){
        print $e->getMessage();
    }
}

class InternalException extends Exception {
  // some code here
}
请参见

“转换”异常是没有意义的。如果你不处理它们,就不要扔

您可以通过以下方式捕获不同的异常:

try {
    throw new InternalException();
} catch (HardwareException $e) {
} catch (InternalException $e) {
    // this catch block will be executed
} catch (Exception $e) {
    // all other exceptions
}

你试过了吗?你在找什么问题?是的。但也许尝试一下会比在这里提问快得多。您可以扩展exception类您面临的错误或问题是什么?为什么要这样做?->有时我有一个服务器错误,我需要在一个日志文件中写入,写入完成后,我想执行一个抛出到全局异常,该异常为用户设置并显示一个简单的用户消息,而不是技术消息。另一个问题是,我喜欢这个外观,但不太喜欢try-catch-multiple-level我想使用这种代码结构try{throw-new-InternalException('Internal');}catch(InternalException$e){throw-new-Exception('Internal-to-global');}catch(Exception$e){print$e->getMessage e();}