程序出现异常请联系技术人员 (程序异常处理方法)

步骤 1 : try catch

步骤 2 : 使用异常的父类进行catch

步骤 3 : 多异常捕捉办法1

步骤 4 : 多异常捕捉办法2

步骤 5 : finally

步骤 6 : throws

步骤 7 : 练习-异常处理

步骤 8 : 答案-异常处理

步骤 1 :

try catch

1.将可能抛出FileNotFoundException 文件不存在异常的代码放在try里

2.如果文件存在,就会顺序往下执行,并且不执行catch块中的代码

3. 如果文件不存在,try 里的代*会码**立即终止,程序流程会运行到对应的catch块中

4. e.printStackTrace(); 会打印出方法的调用痕迹,如此例,会打印出异常开始于TestException的第16行,这样就便于定位和分析到底哪里出了异常

代码比较

package exception; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class TestException { public static void main(String[] args) { File f= new File("d:/LOL*ex.e**"); try{ System.out.println("试图打开 d:/LOL*ex.e**"); new FileInputStream(f); System.out.println("成功打开"); } catch(FileNotFoundException e){ System.out.println("d:/LOL*ex.e**不存在"); e.printStackTrace(); } } }

程序出现异常无法启动主程序,程序异常处理方法

package exception;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class TestException {

public static void main(String[] args) {

File f= new File("d:/LOL*ex.e**");

try{

System.out.println("试图打开 d:/LOL*ex.e**");

new FileInputStream(f);

System.out.println("成功打开");

}

catch(FileNotFoundException e){

System.out.println("d:/LOL*ex.e**不存在");

e.printStackTrace();

}

}

}

步骤 2 :

使用异常的父类进行catch

FileNotFoundException是Exception的子类,使用Exception也可以catch住FileNotFoundException

代码比较

package exception; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class TestException { public static void main(String[] args) { File f= new File("d:/LOL*ex.e**"); try{ System.out.println("试图打开 d:/LOL*ex.e**"); new FileInputStream(f); System.out.println("成功打开"); } catch(Exception e){ System.out.println("d:/LOL*ex.e**不存在"); e.printStackTrace(); } } }

package exception;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class TestException {

public static void main(String[] args) {

File f= new File("d:/LOL*ex.e**");

try{

System.out.println("试图打开 d:/LOL*ex.e**");

new FileInputStream(f);

System.out.println("成功打开");

}

catch(Exception e){

System.out.println("d:/LOL*ex.e**不存在");

e.printStackTrace();

}

}

}

步骤 3 :

多异常捕捉办法1

有的时候一段代*会码**抛出多种异常,比如

new FileInputStream(f);

Date d = sdf.parse("2016-06-03");

这段代码,会抛出 文件不存在异常 FileNotFoundException 和 解析异常ParseException

解决办法之一是分别进行catch

catch (FileNotFoundException e) {

System.out.println("d:/LOL*ex.e**不存在");

e.printStackTrace();

} catch (ParseException e) {

System.out.println("日期格式解析错误");

e.printStackTrace();

}

代码比较

package exception; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestException { public static void main(String[] args) { File f = new File("d:/LOL*ex.e**"); try { System.out.println("试图打开 d:/LOL*ex.e**"); new FileInputStream(f); System.out.println("成功打开"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d = sdf.parse("2016-06-03"); } catch (FileNotFoundException e) { System.out.println("d:/LOL*ex.e**不存在"); e.printStackTrace(); } catch (ParseException e) { System.out.println("日期格式解析错误"); e.printStackTrace(); } } }

package exception;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class TestException {

public static void main(String[] args) {

File f = new File("d:/LOL*ex.e**");

try {

System.out.println("试图打开 d:/LOL*ex.e**");

new FileInputStream(f);

System.out.println("成功打开");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date d = sdf.parse("2016-06-03");

} catch (FileNotFoundException e) {

System.out.println("d:/LOL*ex.e**不存在");

e.printStackTrace();

} catch (ParseException e) {

System.out.println("日期格式解析错误");

e.printStackTrace();

}

}

}

步骤 4 :

多异常捕捉办法2

另一个种办法是把多个异常,放在一个catch里统一捕捉

catch (FileNotFoundException | ParseException e) {

这种方式从 JDK7开始支持,好处是捕捉的代码更紧凑,不足之处是,一旦发生异常,不能确定到底是哪种异常,需要通过instanceof 进行判断具体的异常类型

if (e instanceof FileNotFoundException)

System.out.println("d:/LOL*ex.e**不存在");

if (e instanceof ParseException)

System.out.println("日期格式解析错误");

代码比较

package exception; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TestException { public static void main(String[] args) { File f = new File("d:/LOL*ex.e**"); try { System.out.println("试图打开 d:/LOL*ex.e**"); new FileInputStream(f); System.out.println("成功打开"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d = sdf.parse("2016-06-03"); } catch (FileNotFoundException | ParseException e) { if (e instanceof FileNotFoundException) System.out.println("d:/LOL*ex.e**不存在"); if (e instanceof ParseException) System.out.println("日期格式解析错误"); e.printStackTrace(); } } }

package exception;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

public class TestException {

public static void main(String[] args) {

File f = new File("d:/LOL*ex.e**");

try {

System.out.println("试图打开 d:/LOL*ex.e**");

new FileInputStream(f);

System.out.println("成功打开");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date d = sdf.parse("2016-06-03");

} catch (FileNotFoundException | ParseException e) {

if (e instanceof FileNotFoundException)

System.out.println("d:/LOL*ex.e**不存在");

if (e instanceof ParseException)

System.out.println("日期格式解析错误");

e.printStackTrace();

}

}

}

步骤 5 :

finally

无论是否出现异常,finally中的代码都会被执行

代码比较

package exception; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class TestException { public static void main(String[] args) { File f= new File("d:/LOL*ex.e**"); try{ System.out.println("试图打开 d:/LOL*ex.e**"); new FileInputStream(f); System.out.println("成功打开"); } catch(FileNotFoundException e){ System.out.println("d:/LOL*ex.e**不存在"); e.printStackTrace(); } finally{ System.out.println("无论文件是否存在, 都会执行的代码"); } } }

package exception;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class TestException {

public static void main(String[] args) {

File f= new File("d:/LOL*ex.e**");

try{

System.out.println("试图打开 d:/LOL*ex.e**");

new FileInputStream(f);

System.out.println("成功打开");

}

catch(FileNotFoundException e){

System.out.println("d:/LOL*ex.e**不存在");

e.printStackTrace();

}

finally{

System.out.println("无论文件是否存在, 都会执行的代码");

}

}

}

步骤 6 :

throws

考虑如下情况:

主方法调用method1

method1调用method2

method2中打开文件

method2中需要进行异常处理

但是method2不打算处理,而是把这个异常通过throws抛出去

那么method1就会接到该异常。 处理办法也是两种,要么是try catch处理掉,要么也是抛出去。

method1选择本地try catch住 一旦try catch住了,就相当于把这个异常消化掉了,主方法在调用method1的时候,就不需要进行异常处理了

代码比较

package exception; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; public class TestException { public static void main(String[] args) { method1(); } private static void method1() { try { method2(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void method2() throws FileNotFoundException { File f = new File("d:/LOL*ex.e**"); System.out.println("试图打开 d:/LOL*ex.e**"); new FileInputStream(f); System.out.println("成功打开"); } }

package exception;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

public class TestException {

public static void main(String[] args) {

method1();

}

private static void method1() {

try {

method2();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

private static void method2() throws FileNotFoundException {

File f = new File("d:/LOL*ex.e**");

System.out.println("试图打开 d:/LOL*ex.e**");

new FileInputStream(f);

System.out.println("成功打开");

}

}

关注【前沿梦工场】,回复“吃吃的爱”,“逆天侠盗团2”获取最新电影