方法一
package com.aflfte.exception;
/**
* try catch异常处理方法
*/
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) {
FileReader read=null;
try {
read=new FileReader("f:/b.txt");
char c=(char)read.read();
System.out.println(c);
} catch (FileNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}finally {
try {
if(read!=null) {
read.close();
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
方法二
package com.aflfte.exception;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*使用throws声明异常
*常用于方法中,throws声明后,需要在使用方法时处理异常
* @author jinhao
*
*/
public class Test3 {
public static void main(String[] args) throws IOException {
readFile();
}
public static void readFile() throws IOException {
FileReader read=null;
read=new FileReader("f:/b.txt");
char c=(char)read.read();
System.out.println(c);
if(read!=null) {
read.close();
}
}
}
« 手动处理异常方法
|
枚举的使用»
|