标签:关系 一个 tip 检测 运行时 bounds 语句 system 出现
下图方格内容为java.lang.Object中的类,箭头为继承关系
运行时异常:
此类异常为unchecked(非受查)
即编译不会检测出异常,运行时才会出现的异常
常见的类型有:
编译时异常:
此类异常为checked(受查)
即编译时会检测出异常
常见的类型有:
try{...}
// one or more catch statements
catch (<$EX_CLASS> <$VAR_NAME> [|<$EX_CLASS> <$VAR_NAME> [...]]){...}
finally{...}
note that:
try内声明的变量只能在try内部使用
示例:
public class DealException
{
public static void main(String args[])
{
try
//要检查的程序语句
{
int a[] = new int[5];
a[10] = 7;//出现异常
}
catch(ArrayIndexOutOfBoundsException e)
//异常发生时的处理语句
{
System.out.println("超出数组范围!");
}
finally
//这个代码块一定会被执行
{
System.out.println("*****");
}
System.out.println("异常处理结束!");
}
}
Tips:
|
隔开throw <$EX_OBJ>
其他异常抛出来自代码逻辑,throw是主观意愿的手动抛出异常
两者最终结果的作用相同
出现在函数体中,抛出异常后终止
func throws <$EX_OBJ>
将自身异常提交给上级(调用者/JVM)
从而确保自身在编译过程中不出现异常
public class NoMappingParamString extends Exception {
//无参构造函数
public NoMappingParamString(){
super();
}
//用详细信息指定一个异常
public NoMappingParamString(String message){
super(message);
}
//用指定的详细信息和原因构造一个新的异常
public NoMappingParamString(String message, Throwable cause){
super(message,cause);
}
//用指定原因构造一个新的异常
public NoMappingParamString(Throwable cause) {
super(cause);
}
}
相同:
不同:
标签:关系 一个 tip 检测 运行时 bounds 语句 system 出现
原文地址:https://www.cnblogs.com/arrayblog/p/13252241.html