PHP 的 try-catch

等级 399 次按键

在try块中抛出Exception,并在catch中处理,打印Caught: Error!。

PHP 代码

// 可能抛出异常的代码放在try中
1try {
// throw中断正常执行,跳到匹配的catch
2 throw new Exception("Error!");
// 捕获Exception(及其子类);$e是被抛出的对象
3} catch (Exception $e) {
// getMessage()返回传给构造函数的文本:打印Caught: Error!
4 echo "Caught: " . $e->getMessage();
5}

运行结果

Caught: Error!
用这段代码练习打字

可以用这段代码练习打字。成绩不会计入排行榜。

这段代码中的符号

这段 PHP 代码中需要输入的符号及其出现次数。

  • "× 4
  • (× 3
  • )× 3
  • ;× 2
  • {× 2
  • }× 2
  • $× 2
  • -× 1
  • :× 1
  • !× 1

PHP 等级 3 的更多代码示例

← 返回 PHP 打字练习