JAVA - 基础 - Java异常

异常处理try-catch-finally,throw,assert,异常类,方法。

1

try{

    //可能出现异常的代码

}

Catch(异常类 异常对象){

    //捕捉异常

//处理异常的代码

//异常类方法

printStackTrace();//打印方法调用堆栈

 

getMessage();返回异常相关信息

}

//可以有多个捕捉块,但异常类是Exception时不能有其子类异常捕捉

Catch(){
}

……

Finally{

   //最终执行的代码,也是一定执行的代码

   //但不一定要执行finally语句(我这有2种情况):如例3

   //finall中语句也可能出错,那时将放弃先前的异常

}

 

 //在结束时

 //自动释放资源,close()自动调用

*Java多层嵌套异常处理流程*

2

 

 

3

处理方式

 

Catch捕捉(exception),

Throw:语句,抛出异常对象(exception

 throw语句抛出受控的异常,可抛出多个异常-捉到一个就可以。

继承中:在子类中throw语句抛出的异常类不能是父类中throw舆抛出的异常类的父类。

Assert语句,可在判断条件时,抛出系统错误(error)。

 

4特别的错误(error-OOMOutOfMemoryError

内存不足

 

5实验

1

public class ExceptionT {

 

public static void main(String[] args) {

try {

int i=2;

int j=0;

int k=i/j;

System.out.println("k="+k);

}catch(Exception e) {

System.out.println("error1");

}

try {

double i=2;

double j=0;

double k=i/j;//不报错

System.out.println("k="+k);//k=Infinity;

                           //两处JVM生成的字节码不一样

}catch(Exception e) {

System.out.println("error2");

}

 

}

2:

public class ExceptionT {

public static void main(String[] args) {

try {

int i=2;

int j=0;

int k=i/j;

System.out.println("k="+k);

}catch(Exception e) {

System.out.println("error1");

}finally{

int i1=2;

int i2=0;

System.out.println("k="+i1/i2);//出错

}

}

}

 

 

3:多层捕获异常

Text1:

public class CatchWho {

    public static void main(String[] args) {

        try {

             try {

                 throw new ArrayIndexOutOfBoundsException("error1");

             }

             catch(ArrayIndexOutOfBoundsException e) {

                System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");

             }

 

            throw new ArithmeticException("error2");

        }

        catch(ArithmeticException e) {

            System.out.println("发生ArithmeticException");

        }

        catch(ArrayIndexOutOfBoundsException e) {

           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");

        }

    }

}

 

Text2

public class CatchWho2 {

    public static void main(String[] args) {

        try {

             try {

                 throw new ArrayIndexOutOfBoundsException("error1");

             }

             catch(ArithmeticException e) {

                 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");

             }

            throw new ArithmeticException("error2");

        }

        catch(ArithmeticException e) {

            System.out.println("发生ArithmeticException");

        }

        catch(ArrayIndexOutOfBoundsException e) {

            System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");

        }

    }

}

 

Text3

 

public class EmbededFinally {

public static void main(String args[]) {

int result;

try {          

System.out.println("in Level 1");

  try {

                

System.out.println("in Level 2");

                // result=100/0;  //Level 2

               

  try {

  System.out.println("in Level 3");     

  result=100/0;  //Level 3

}

catch (Exception e) {

System.out.println("Level 3:" + e.getClass().toString());

}

finally {   

System.out.println("In Level 3 finally");

}

// result=100/0;  //Level 2

}

catch (Exception e) {

  System.out.println("Level 2:" + e.getClass().toString());

  }

  finally {     

System.out.println("In Level 2 finally");

 }  

 //result = 100 / 0;  //level 1

}

catch (Exception e) {

System.out.println("Level 1:" + e.getClass().toString());

}

finally {

  System.out.println("In Level 1 finally");

}

    

}

}

多层嵌套try-catch-finally

(1) 捕捉异常对象与抛出的异常类型相同,

(2) 有抛出异常时,先接受,结束程序

(3) finally语句不一定调用,有时在嵌套的外层出现异常并捕捉后内层的try-catch-finally就不执行了。

第二中是System.exit(0);程序终止语句出现。

 

实验:

import java.util.Scanner;

public class MyException {

 

public static void main(String args[]) {

System.out.println("输入你的成绩:");

Scanner scan=new Scanner(System.in);

try{

int score=scan.nextInt();

if(score<90) {

if(score<80) {

if(score<70) {

if(score<60) {

System.out.println("成绩不及格");

}

System.out.println("成绩及格");

}

System.out.println("成绩中");

}

System.out.println("成绩良");

}

System.out.println("成绩优");

}catch(Exception e) {

System.out.println("输入错误。输入一个整数。");

}

}

}

 

posted @ 2017-11-19 13:31  丨Kouch  阅读(104)  评论(0编辑  收藏  举报