小皇帝闹闹

9.异常处理

一个try对应一个catch,一个catch对应多个try.catch只能捕获Exception类及其子类的对象。

finally语句一定执行。

一.运行AboutException.java

  

二.尝试解释现象:double d1=100,d2=0,result; result=d1/d2; System.out.print(“浮点数除以零:”+data在运行时不会引发异常,int i=1,j=0,k; k=i/j;在运行时会引发异常。

  答:浮点数的计算与整数的计算不同。

三.运行程序CatchWho.java.写出结果

  

四.运行程序CatchWho2.java.写出结果

  

五.运行EmbedFinally.java输出结果总结。

  

   Try,catch,finally相互嵌套时,先处理最内层的try-catch-finally。当try抛出了与catch匹配的异常,则代码到相应的catch()中执行。如果catch也出现了异常,程序会检测finally中是否有异常,若有,则覆盖。如果只有try-finally,那么先执行finally,如果finally没有异常,则返回处理try中的异常,如果finally有异常,则覆盖try中的异常

六.运行SystemExitAndFinally.javafinally语句块一定执行吗?

  

  finally语句块一定执行。

 

七.下列代码为何不能通过编译?

  public class TestThrows   {

public static void main(String[] args)     {

FileInputStream fis = new                       

            FileInputStream("a.txt");

}

}

  Throws受控异常,需要在调用此方法的代码中使用try,catch,finally,进行捕获,或者重新声明。

八.抛出多个受控异常

  int g(float h) throws OneException,TwoException

{ …… }

九.一个子类的throws子句抛出的异常,不能是其父类同名方法(test)抛出的异常对象的父类,

  public class OverrideThrows

{

public void test()throws IOException

{

FileInputStream fis = new FileInputStream("a.txt");

}

}

class Sub extends OverrideThrows

{

public void test() throws FileNotFoundException

{

            ...

}

}

十.自定义异常。

  Class MyException extends Exception

  {    }

  在需要处理的地方使用throws语句抛出自定义异常

  Void method()

  {
    if(  )

      Throws new MyException();

  }

十一.编写一个程序。要求用户输入一个整数,代表某一门课的考试成绩,接着给出不及格,及格,中,良,优的结论。不管输入什么内容程序都不会奔溃。

  源代码://编写一个程序。要求用户输入一个整数,代表某一门课的考试成绩,接着给出不及格,及格,中,良,优的结论。不管输入什么内容程序都不会奔溃。

//20153250    信1503  陈欣容

package demo;

import java.util.Scanner;

class Panduan1 extends Exception     //输入不是整数

{

public Panduan1(String str)

{ super(str);}

}

class Panduan2 extends Exception     //输入分数不正确

{

public Panduan2(String str)

{ super(str);}

}

public class Test {

public static void main(String args[])

{

boolean boo=true;

while(boo)

{

Scanner in=new Scanner(System.in);

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

String num=in.next();

try

{

for(int i=0;i<num.length();i++)

  {

if(num.charAt(i)<48||num.charAt(i)>57)

{

throw new Panduan2("输入不是整数,请重新输入");

}

  }

try

{

int s=Integer.parseInt(num);

if(s<0||s>100)

 {

throw new Panduan1("输入的分数不正确,请重新输入");

 }

if(s<60)

  { System.out.println("不及格"); }

if(s>=60&&s<70)

  { System.out.println("及格");}

if(s>=70&&s<80)

  { System.out.println("中"); }

if(s>=80&&s<90)

  { System.out.println("良"); }

if(s>=90&&s<=100)

  { System.out.println("优"); }

}

catch(Panduan1 e)

{

System.out.println(e.getMessage());

}

  }

catch(Panduan2 e)

{

System.out.println(e.getMessage());

}

}

}

}

  截图:

  总结:一抛一接。多个trycatch时,先写内部的trycatch

       判断输入不是数字:输入num   

        for(int i=0;i<num.length();i++)

  { if(num.charAt(i)<48||num.charAt(i)>57)

           System.out.print(“不是”)}

       System.out.print()是输出。System.out.println()是输出并换行。

         自定义异常的结构:

          class Panduan extends Exception     //输入不是整数

          {

       public Panduan(String str)

       { super(str);}

         }

        Try

        {

          ......

          Throw new Panduan(“......”);

        }

        catch(Panduan e)

{

System.out.println(e.getMessage());

}

posted on 2016-11-25 17:23  小皇帝闹闹  阅读(119)  评论(0编辑  收藏  举报

导航