RuntimeException 类

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

简介

如果发生只能在运行时发现的错误,则抛出此异常。

类概要

class RuntimeException extends Exception {
/* 继承的属性 */
protected string $message = "";
private string $string = "";
protected int $code;
protected string $file = "";
protected int $line;
private array $trace = [];
private ?Throwable $previous = null;
/* 继承的方法 */
public Exception::__construct(string $message = "", int $code = 0, ?Throwable $previous = null)
final public Exception::getCode(): int
final public Exception::getFile(): string
final public Exception::getLine(): int
final public Exception::getTrace(): array
}
添加注释

用户贡献的注释 3 个注释

-4
sricharan dot krishnan at gmail dot com
8 年前
使用 RuntimeException 类的简单示例:-

假设我们要对两个数字进行除法,并在分母等于零时抛出异常。

<?php
$iNum1
= 10;
$iNum2 = 0;

try{
if (
$iNum2 == 0){
throw new
RuntimeException("Division by Zero");
}
$iResult = $iNum1 / $iNum2;
echo (
"Division Result of \$iNum1 and $iNum2 = ".($iResult)."<br/>");
}
catch (
RuntimeException $e){
echo (
"Division by Zero is not possible");
}
?>
-12
Dawid Krysiak
13 年前
直接已知子类(遵循 java 文档约定 (: )
OutOfBoundsException, OverflowException, RangeException, UnderflowException, UnexpectedValueException
To Top