1、体系结构
java.lang.Object
|--java.lang.Throwable
|-- java.lang.Error:错误,java程序对此无能为力,不显示的处理
|---java.lang.Exception: 异常,需要进行处理
|--RuntimeException; 运行时异常
|--ArrayIndexOutofBoundsException,NullPointerException,ArithmeticException,ClassCastException
|--
|--非RuntimeException:编译时异常
2、因为java程序分为javac.exe和java.exe两个过程,在每个过程中,都有可能出现异常,故分为编译、运行时异常。
2.1、对于运行时异常比较常见,可以不显示的来处理。
2.2、对于编译时异常,必须显式的处理
编译时异常,不是说有异常才处理,而是存在异常的隐患,因此需要进行显式的处理。
3、如何处理异常
抓抛模型
3.1、抛
执行代码时,出现异常,就会在异常的代处生成一个对应的异常对象,并将此对象抛出。
一旦抛出此异常类的对象,那么程序就会终止执行。
此异常类的对象抛给方法的调用者
3.2、抓
如何抓?异常处理方式
1、try{
// 可能出现异常的代码
}catch(Exception1 e1){
// 处理方式1
}catch (Exception2 e2){
// 处理方式2
}finally{
// 一定要执行的代码
}
// 1、数据越界 ArrayIndexOutofBoundsException
@Test
public void test1(){
int[] i = new int[10];
try{
System.out.println(i[10]);
}catch(IndexOutOfBoundsException e){
System.out.println("数组越界");
}
// System.out.println(i[-10]);
}
注:
1、try内申明的变量类似于局部变量,出了try{}语句就不能被调用。
2、finally 可选
3、catch语句内部是异常对象的处理。
3.1、getMessage()
3.2、printStackTrace() 右键surround with-->try/catch() block,
public void test2(){
try {
int i = 10;
System.out.println(i/0);
} catch (Exception e) {
e.printStackTrace();
}
}
java.lang.ArithmeticException: / by zero
at Exception.java.TestException1.test2(TestException1.java:26)
at
3.3、自定义方式处理
4、可以有多个catch语句,try中抛出的异常类的对象从上往下匹配catch中的异常的类型,一旦满足就执行catch中的代码,就跳出其后得的多条catch语句.
5、如果异常处理了,其后的代码继续执行.
6、若catch中多个异常并列关系,孰上孰下都可以;
6.1、若是catch中多个异常是包含关系,必须将子类放在父类的上面,否则出现编译异常。
7、finally中存放的是一定被执行的代码,不管try/catch中仍有异常未处理,已经是否return语句。
@Test
public void test6() {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("hello.txt"));
int b;
while ((b = fis.read()) != -1) {
System.out.println((char) b);
}
} catch (FileNotFoundException e1) {
System.out.println(e1.getMessage());
} catch (IOException e1) {
System.out.println(e1.getMessage());
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
8、try/catch是可以相互嵌套的。
4、对于运行时异常来说,可以不显式的进行处理, 对于编译时异常,则必须进行显式处理。
// 编译时异常:在编译的时候就要显示的进行异常处理
@Test
public void test5(){
try{
FileInputStream fis = new FileInputStream(new File("hello.txt"));
int b;
while((b = fis.read()) != -1){
System.out.println((char)b);
}
}catch(FileNotFoundException e1){
System.out.println(e1.getMessage());
}catch(IOException e1){
System.out.println(e1.getMessage());
}finally{
try{
fis.close();
}catch(IOException e1){
System.out.println(e1.getMessage());
}
}
5、执行一个程序时,如果出现异常,则异常之后的代码不会被执行。
java.lang.Object
|--java.lang.Throwable
|-- java.lang.Error:错误,java程序对此无能为力,不显示的处理
|---java.lang.Exception: 异常,需要进行处理
|--RuntimeException; 运行时异常
|--ArrayIndexOutofBoundsException,NullPointerException,ArithmeticException,ClassCastException
|--
|--非RuntimeException:编译时异常
2、因为java程序分为javac.exe和java.exe两个过程,在每个过程中,都有可能出现异常,故分为编译、运行时异常。
2.1、对于运行时异常比较常见,可以不显示的来处理。
2.2、对于编译时异常,必须显式的处理
编译时异常,不是说有异常才处理,而是存在异常的隐患,因此需要进行显式的处理。
3、如何处理异常
抓抛模型
3.1、抛
执行代码时,出现异常,就会在异常的代处生成一个对应的异常对象,并将此对象抛出。
一旦抛出此异常类的对象,那么程序就会终止执行。
此异常类的对象抛给方法的调用者
3.2、抓
如何抓?异常处理方式
1、try{
// 可能出现异常的代码
}catch(Exception1 e1){
// 处理方式1
}catch (Exception2 e2){
// 处理方式2
}finally{
// 一定要执行的代码
}
// 1、数据越界 ArrayIndexOutofBoundsException
@Test
public void test1(){
int[] i = new int[10];
try{
System.out.println(i[10]);
}catch(IndexOutOfBoundsException e){
System.out.println("数组越界");
}
// System.out.println(i[-10]);
}
注:
1、try内申明的变量类似于局部变量,出了try{}语句就不能被调用。
2、finally 可选
3、catch语句内部是异常对象的处理。
3.1、getMessage()
3.2、printStackTrace() 右键surround with-->try/catch() block,
public void test2(){
try {
int i = 10;
System.out.println(i/0);
} catch (Exception e) {
e.printStackTrace();
}
}
java.lang.ArithmeticException: / by zero
at Exception.java.TestException1.test2(TestException1.java:26)
at
3.3、自定义方式处理
4、可以有多个catch语句,try中抛出的异常类的对象从上往下匹配catch中的异常的类型,一旦满足就执行catch中的代码,就跳出其后得的多条catch语句.
5、如果异常处理了,其后的代码继续执行.
6、若catch中多个异常并列关系,孰上孰下都可以;
6.1、若是catch中多个异常是包含关系,必须将子类放在父类的上面,否则出现编译异常。
7、finally中存放的是一定被执行的代码,不管try/catch中仍有异常未处理,已经是否return语句。
@Test
public void test6() {
FileInputStream fis = null;
try {
fis = new FileInputStream(new File("hello.txt"));
int b;
while ((b = fis.read()) != -1) {
System.out.println((char) b);
}
} catch (FileNotFoundException e1) {
System.out.println(e1.getMessage());
} catch (IOException e1) {
System.out.println(e1.getMessage());
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
8、try/catch是可以相互嵌套的。
4、对于运行时异常来说,可以不显式的进行处理, 对于编译时异常,则必须进行显式处理。
// 编译时异常:在编译的时候就要显示的进行异常处理
@Test
public void test5(){
try{
FileInputStream fis = new FileInputStream(new File("hello.txt"));
int b;
while((b = fis.read()) != -1){
System.out.println((char)b);
}
}catch(FileNotFoundException e1){
System.out.println(e1.getMessage());
}catch(IOException e1){
System.out.println(e1.getMessage());
}finally{
try{
fis.close();
}catch(IOException e1){
System.out.println(e1.getMessage());
}
}
5、执行一个程序时,如果出现异常,则异常之后的代码不会被执行。