异常处理
try
{
}
catch(Exception e)
{
alert 输入不合法
goto
}
异常类的继承体系
- 非正常:异常(Exception)和错误(Error)
- IndexOutOfBoundsException:数组越界
- NumberFormatException:数组格式错误
- ArithmeticException: 除0异常
- NullPointerException: 空指针异常
java7后可以捕获多个异常
访问异常信息
finally回收资源
- 若try中打开了物流资源,则需要finally回收
Cheked异常
Runtime异常
throw抛出异常
- 当前方法不知道如何处理这种类型的异常,throw,然后交给JVM,jvm跟踪栈信息。
public class ThrowsTest
{
public static void main(Srting[] args)
throws IOException
{
FileInputStream fis=new FileInputStrean("a.txt")
}
}
- 不跟踪异常,将异常交给jvm
- jam打印异常,结束程序
catch和throw的使用
异常链