imooc java学习之异常与异常处理作业

来源:互联网 发布:淘宝动漫周边那家店好 编辑:程序博客网 时间:2024/05/16 05:25

    一个模拟图书馆使用书名或者编号查找书的问题,很简单的问题,在写的过程中还是遇到了很多问题,不过却对知识有了进一步的了解,还是有些窃喜吐舌头


参考的博客:

http://blog.csdn.net/hguisu/article/details/6155636

http://blog.csdn.net/wjy1090233191/article/details/42080029

这两位博主写得还是很详细的,写的特别棒。


首先说一下对异常的认识:

    1.try,catch,finally

      try用于捕获异常,将try块中的异常抛出;catch用于处理try捕获到的异常。try后面可以跟0个,1个或多个catch,如果try后面没有catch,则必须跟一个finally语句。

      try{

         //可能会有异常

      }catch(type1 e){

         //捕获try中type1类型的异常

      }catch(type2 e){

        //捕获try中type2类型的异常

      }

    注意:

   1)如果异常被某个catch块捕获了,try-catch语句就结束了!!!catch的异常类型必须根据类型从小到大的顺序写,因为如果把范围大的写在前面,那么异常就不会被后面的捕获了。

    2)在try-catch-finally语句中还可以重新抛出异常哦。

    3)在try中出现异常时,其后面的代码就不会被执行了,比如是输入错误,应该输入int类型的数,而输入确是字符或者字符串,那么就会抛出异常,而且,注意啦,你输入的这个内容并没有被接收。这也是我在敲代码的时候错了好久才get到的。

    finally语句跟在catch后,不管有无异常,他都会执行。可以写一些必须包含的代码,比如关闭指针什么的。

    finally的执行时间:try-catch执行结束之后,方法返回之前(即使try-catch语句中有return语句,也会在返回之前执行finally)。

    在以下几种情况finally不会被执行:

    1)在finally语句块中发生了异常。
    2)在前面的代码中用了System.exit()退出程序。
    3)程序所在的线程死亡。
    4)关闭CPU。

    2.throw,throws

    首先要说的是这两个是不同的东西,并没有什么联系。throw是一个动作,用于抛出一个异常;而throws用于方法的异常声明。

    1)Java不要求在方法中显示声明Error和RuntimeException(免检异常),但是,方法要抛出其他异常必须在方法头中显示声明,这样,方法的调用者会被告知有异常。

     注意:如果方法没有在父类中声明异常,那么就不能在子类中对其进行覆盖来声明异常。

    2)如果一个方法A中调用了另一个方法B(该方法抛出了异常),那么如果方法A没有解决这个异常,那么他必须声明抛出异常。例:

    f1()throws Exception{

    }

    //编译错误,f1()有异常抛出f2()需要声明抛出,或者捕获

    f2(){

    f1()

    }

    //正确

    f3()throws Exception{

    f1()

    }  

    //正确 

    f3(){

    try{

    f1()

        }catch(Exception e){

         }

    }

接着说一下我在敲代码时遇到的有趣的情况:

     

java中Scanner类nextInt之后用nextLine无法读取输入,好神奇,这是为什么呢

     首先,Scanner是一个扫描器,它扫描数据都是去内存中一块缓冲区中进行扫描并读入数据的,而我们在控制台中输入的数据也都是被先存入缓冲区中等待扫描器的扫描读取。这个扫描器在扫描过程中判断停止的依据就是“空白符”,空格啊,回车啊什么的都算做是空白符。

nextInt()方法在扫描到空白符的时候会将前面的数据读取走,但会丢下空白符“\r”在缓冲区中,但是,nextLine()方法在扫描的时候会将扫描到的空白符一同清理掉。

所以,在nextint之后,nextLine接收的是前面输入遗留的回车,并不是我们要的数据。

具体的情况我已经标注在代码中了,可以看着代码理解一下。

最后再记录一下大神博客中的一道题

package Test;    public class TestException {      public TestException() {      }        boolean testEx() throws Exception {          boolean ret = true;          try {              ret = testEx1();          } catch (Exception e) {              System.out.println("testEx, catch exception");              ret = false;              throw e;          } finally {              System.out.println("testEx, finally; return value=" + ret);              return ret;          }      }        boolean testEx1() throws Exception {          boolean ret = true;          try {              ret = testEx2();              if (!ret) {                  return false;              }              System.out.println("testEx1, at the end of try");              return ret;          } catch (Exception e) {              System.out.println("testEx1, catch exception");              ret = false;              throw e;          } finally {              System.out.println("testEx1, finally; return value=" + ret);              return ret;          }      }        boolean testEx2() throws Exception {          boolean ret = true;          try {              int b = 12;              int c;              for (int i = 2; i >= -2; i--) {                  c = b / i;                  System.out.println("i=" + i);              }              return true;          } catch (Exception e) {              System.out.println("testEx2, catch exception");              ret = false;              throw e;          } finally {              System.out.println("testEx2, finally; return value=" + ret);              return ret;          }      }        public static void main(String[] args) {          TestException testException1 = new TestException();          try {              testException1.testEx();          } catch (Exception e) {              e.printStackTrace();          }      }  }  

这个题一看就写出了错误的答案:

i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, catch exception
testEx1, finally; return value=false
testEx, catch exception
testEx, finally; return value=false

正确答案为:

i=2
i=1
testEx2, catch exception
testEx2, finally; return value=false
testEx1, finally; return value=false
testEx, finally; return value=false


原因:finally中使用return是一种很不好的编程风格,它会覆盖掉所有的其它返回,并且吃掉catch中抛出的异常。对于调用这个方法的方法来说,接收到了返回,就相当于方法正常执行了,是没有异常的,所以方法抛出的异常被忽略了。


import java.util.*;
import java.io.*;public class findBook {private String[] bookName={"高数","英语","C"};Scanner input = new Scanner(System.in);/** * @param args */public static void main(String[] args) throws IOException{// TODO Auto-generated method stubfindBook bk=new findBook();while(true){System.out.println("输入命令:1-按照书名查找图书,2-按照序号查找图书");try{int num=bk.getCommond();switch(num){case 1:bk.name();break;case 2:bk.Number();break;case -1:System.out.println("请输入指定的数字");break;default:System.out.println("输入错误!!!");break;}}catch(Exception e){System.out.println("请重新输入");continue;}}}public int getCommond()throws IOException{int num;try{num=input.nextInt();return num;}catch(Exception e){input.next();return -1;}}//按照书名查找public void name() throws Exception{String strname;System.out.print("请输入书名:");//input.nextLine();strname=input.nextLine();   //如果输入用nextLine的话就无法接收到输入for(int i=0;i<3;i++){if(strname.equals(bookName[i])){System.out.println("找到了"+strname);return;}}throw new Exception("图书不存在!");}//按照序号查找public void Number()throws Exception{while(true){System.out.println("请输入书号:");try{int index=input.nextInt();System.out.println("找到了"+bookName[index]); }catch(ArrayIndexOutOfBoundsException e){System.out.println("图书不存在");throw new Exception("BookNotExist");}catch(Exception e){System.out.println("输入格式错误,请根据提示输入!!!");input.next();//如果输入是非数字的话,输入就未被接收故须加一个接收continue;}}}}


    

     

0 0
原创粉丝点击