异常:程序在运行时出现的不正常现象
Throwable
|——Error
|——Exception
严重级别:Error类和Exception类
异常的处理:try{}catch{}finally{}
public class VariableDemo { /**
* @param args
*/
public static void main(String[] args) {
try {
System.out.println(1/0);
System.out.println("此处不会执行");
} catch (Exception e) {
e.printStackTrace();
System.out.println("异常捕获");
}finally{
System.out.println("异常finally");
} /**
* 输出:
* java.lang.ArithmeticException: / by zero
at VariableDemo.main(VariableDemo.java:9)
异常捕获
异常finally
*/
} }
PHP中的异常捕获,必须手动抛异常,并且finally是在PHP5.5以上才有
<?php function getNum($a){
if(!$a){
throw new Exception("Division by zero.");
}
return 10/$a;
}
try{
echo getNum(0); }catch(Exception $e){
echo $e->getMessage();
echo "异常捕获";
} finally {
echo "异常finally";
} /**
*输出
*Division by zero. 异常捕获 异常finally
*/