------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------
异常
一概述
异常:在Java语言中,将程序执行中发生的不正常情况称为“异常”。(开发过程中的语法错误和逻辑错误不是异常)
Java程序在执行过程中所发生的异常事件可分为两类:
Error: Java虚拟机无法解决的严重问题。如:JVM系统内部错误、资源耗尽等严重情况。一般不编写针对性的代码进行处理。Exception:其它因编程错误或偶然的外在因素导致的一般性问题,可以使用针对性的代码进行处理。
Exception可以分为编译时异常和运行时异常。
二运行时异常RuntimeException
运行时异常:是指编译器不要求强制处置的异常。一般是指编程时的逻辑错误,是程序员应该积极避免其出现的异常。java.lang.RuntimeException类及它的子类都是运行时异常。
常见的运行时异常有:数组下标越界异常,算术异常,类型转换异常,空指针异常。
<span style="font-size:18px;"><span style="font-size:18px;">//1.数组下标越界的异常:ArrayIndexOutOfBoundsException
public void test2(){
int[] i = new int[10];
System.out.println(i[10]);
}</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">//2.算术异常:ArithmeticException
public void test3(){
int i = 10;
System.out.println(i / 0);
}</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">//3.类型转换异常:ClassCastException
public void test4(){
Object obj = new Date();
String str = (String)obj;
}</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">//4.空指针异常:NullPointerExcetion
public void test5(){
String str = new String("AA");
str = null;
System.out.println(str.length());
}
</span></span>
三异常处理
Java提供的是异常处理的抓抛模型。Java程序的执行过程中如出现异常,会生成一个异常类对象,该异常对象将被提交给Java运行时系统,这个过程称为抛出(throw)异常。
异常对象的生成:
由虚拟机自动生成:程序运行过程中,虚拟机检测到程序发生了问题,如果在当前代码中没有找到相应的处理程序,就会在后台自动创建一个对应异常类的实例对象并抛出——自动抛出。由开发人员手动创建:Exceptionexception =new ClassCastException();——创建好的异常对象不抛出对程序没有任何影响,和创建一个普通对象一样
如果一个方法内抛出异常,该异常对象会被抛给调用者方法中处理。如果异常没有在调用者方法中处理,它继续被抛给这个调用方法的上层方法。这个过程将一直继续下去,直到异常被处理。这一过程称为捕获(catch)异常。
如果一个异常回到main()方法,并且main()也不处理,则程序运行终止。
异常处理方式一try-catch-finally
try{
...... //可能产生异常的代码
}
catch( ExceptionName1 e ){
...... //当产生ExceptionName1型异常时的处置措施
}
catch( ExceptionName2 e ){
...... //当产生ExceptionName2型异常时的处置措施
}
finally{
...... //无论是否发生异常,都无条件执行的语句,一般用来关闭资源
}
<span style="font-size:18px;"><span style="font-size:18px;">public class TestException1 {
public void test6() {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("hello.txt"));
int b;
while ((b = fis.read()) != -1) {
System.out.print((char) b);
}
} catch (FileNotFoundException e1) {
System.out.println("文件找不到了!");
} catch (IOException e1) {
System.out.println(e1.getMessage());
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}</span></span>
异常处理方式二—throws
如果一个方法(中的语句执行时)可能生成某种异常,但是并不能确定如何处理这种异常,则此方法应显示地声明抛出异常,表明该方法将不对这些异常进行处理,而由该方法的调用者负责处理。在方法声明中用throws语句可以声明抛出异常的列表,throws后面的异常类型可以是方法中产生的异常类型,也可以是它的父类。
<span style="font-size:18px;">public class TestException {
public static void main(String[] args){
try{
method2();
}catch(FileNotFoundException e){
System.out.println(e.getMessage());
}catch(IOException e){
e.printStackTrace();
}
method3();
}
public static void method3(){
try{
method1();
}catch(Exception e){
e.printStackTrace();
}
}
public static void method2() throws FileNotFoundException,IOException{
method1();
}
public static void method1() throws FileNotFoundException,IOException{
FileInputStream fis = new FileInputStream(new File("hello1.txt"));
int b;
while((b = fis.read()) != -1){
System.out.print((char)b);
}
fis.close();
}
}</span>
四自定义异常
自定义类继承Exception或者其子类。
通过构造函数定义异常信息。
例:
通过构造函数定义异常信息。
例:
Class DemoException extends Exception
{
DemoException(String message)
{
super(message);
}
}
通过throw将自定义异常抛出。