java 异常 之 实战篇(trows 和 try catch Dead Code)

一:throws  trycatch 差别

(1)比如。publicFileWriter(String fileName) throws IOException{}

我在mian中创建一个FileWrite对象
importjava.io.*;
publicclass ShengmingThrows {
     public static void main(String[] args){
         try{
         FileWriter fw=new FileWriter("k.txt");
         } catch(
FileNotFoundException ex){}
    }
}

(2) 还有一种处理方法:
importjava.io.*;
publicclass ShengmingThrows {
     public static void main(String[] args)throws IOException{
        //try{
         FileWriter fw=new FileWriter("k.txt");
       // }
       // catch(IOException e){}
    }
}
请解释一下两种操作的不同之处。throws仅仅是声明异常,异常并没有处理throws仅仅是对该类声明会抛出一个异常,但没有对异常进行捕获,让别的调用它的方法进行处理。

或者继续抛出,抛给上一层函数或类的成员函数。。

try catch是对可能出现异常的代码捕获异常并对其进行处理

throws仅仅是声明异常,异常并没有处理
当然。try和
catch也不是非要处理啊。
像代码,
try{
        FileWriter fw=new FileWriter("k.txt");
        }
        catch(IOException e){}   //
catch里也是空的,它也没有处理啊

二:抛出什么异常。如捕获多个异常

(1)完整代码例如以下

File file = new File("d:\\a.txt");// 这个不是read的不会抛异常的,仅仅有。有可能抛出对应的异常,才写catch的,否则多余的catch eclipse也会报错的
BufferedReader bf = new BufferedReader(new FileReader(file));//有可能抛异常
catch(ParseException ex){// Date d1 = df.parse(tmp_date + t1);是因为parse函数抛出的异常,所以编程中能够依据函数的提示来书写异常
			ex.printStackTrace();
			System.out.println("数据解析异常:" + ex);
			// log.warn("****" + ex);
		}
	public static void main(String[] args){
		DateFormat df = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
		
		String t1 = "07:30:45";
		String t2 = "08:32:46";
		String tmp_date = "2014-04-01 ";
		try{
			File file = new File("d:\\a.txt");// 这个不是read的不会抛异常的。仅仅有,有可能抛出对应的异常,才写catch的。否则多余的catch eclipse也会报错的
			BufferedReader bf = new BufferedReader(new FileReader(file));
			Date d1 = df.parse(tmp_date + t1);
			Date d2 = df.parse(tmp_date + t2);
			//System.out.println("******" + d1.compareTo(d2));
			System.out.println(d1.getTime());
			System.out.println(d2.getTime());
			long diff = d2.getTime()-d1.getTime();
			long hour = diff/(1000*60*60);
			diff = diff%(1000*60*60);
			long minute = diff/(1000*60);
			diff = diff%(1000*60);
			long second = diff/1000;
			
			System.out.println("hour=" + hour + ", minute=" + minute + ", second=" + second);//2685000
			
		}catch(ParseException ex){// Date d1 = df.parse(tmp_date + t1)是因为parse函数抛出的异常,所以编程中能够依据函数的提示来书写异常
			ex.printStackTrace();
			System.out.println("数据解析异常:" + ex);
			// log.warn("****" + ex);
		}catch(NullPointerException ex){
			ex.printStackTrace();
			System.out.println("空指针异常:" + ex);
			// log.warn("****" + ex);
		}catch(IndexOutOfBoundsException ex){
			ex.printStackTrace();
			System.out.println("数组越界异常:" + ex);
			// log.warn("****" + ex);
		}catch(RuntimeException ex){
			ex.printStackTrace();
			System.out.println("执行时异常,NullPointerException IndexOutOfBoundsException 都是其子类" + ex);
			// log.warn("****" + ex);
		}
		catch(FileNotFoundException ex){
			ex.printStackTrace();
			System.out.println("文件找不到异常:" + ex);
			// log.warn("****" + ex);
		}catch(IOException ex){
			ex.printStackTrace();
			System.out.println("IO读取异常,是FileNotFoundException的父类" + ex);
			// log.warn("****" + ex);
		}catch(Exception ex){
			ex.printStackTrace();
			System.out.println("异常,上面各种异常的父类" + ex);
			// log.warn("****" + ex);
		}
	}

}

(2)总之,由于Exception这个是那几个异常的 父类或者基类。那几个异常都是他的子类,Exception放在最前面后面就没有机会了。其把全部的异常都捕获了。

三:初遇Dead Code

(1) Dead Code引起的原因

常常使用MyEclipseEclipse编辑器编写java代码的程序猿,可能常常遇到一个黄线警告提示:dead code;一般程序猿遇到这些问题都会置之不理,反正也不影响程序的

编译运行。对,这不是bug,仅仅是一个提示,对于一个有强迫症的程序猿来说,他非要代码一点问题都没有,包含黄线警告都要消灭掉。这里简单说下dead code 即死代

码、无作用的代码提示的原因和解决方法。

顾名思义,死代码,即你编写的那一行是无效代码,可有可无,说白了就是一行废话。这是你就要看一下这一行的处理逻辑是什么。可能是多余推断或者其它多余代码;比

如例如以下情况:

(2)情况一:无用的条件推断。就是你推断的这个条件永远为真

if (true& true) {

    System.out.println("execute ok");

  } else {

    System.out.println("executefail");

  }

 从else開始即为无作用。由于true&true在编绎时即知结果,所以else部分是没用的。编译器知道肯定不会运行的代码。

换成:

boolean a =true;

  boolean b = true;

  if (a & b) {

    System.out.println("execute ok");

  } else {

    System.out.println("executefail");

  }

则不会出现该问题。由于在编译时编译器不确定a & b是否恒成立。

(2)情况二:多余的推断,就是你推断的这个对象永远不为空;事实上类似于情况一

  TimeLineEventModel dataModel = new TimeLineEventModel();

if(dataModel !=null){

    运行一些操作....

}

这里的推断也是多余,由于你已经new了这个对象,那这个对象就不会为空。你刚刚new的对象。怎么会为空呢?

未完待续,可能还有其它一些情况的死代码。等到时候编码遇见了再进行补充吧!

眼下来看。deadcode提示一般出如今if或其它推断的条件上。

posted @ 2016-01-30 10:47  mfrbuaa  阅读(631)  评论(0编辑  收藏  举报