
这是课程里给的代码,自己看着讲义在里面加了些注释。
import java.io.*;
class TryWithResourcesTest {
// 可以抛出子类异常(更具体的异常),但不能抛出更一般的异常
public static void main(String ... args)
throws IOException
{
String path = "c:\aaa.txt";
System.out.println( ReadOneLine1( path ) );
System.out.println( ReadOneLine2( path ) );
}
static String ReadOneLine1(String path){
BufferedReader br=null;
try {
br=new BufferedReader(new FileReader(path));
return br.readLine();
} catch(IOException e) {
e.printStackTrace();
} finally {
if(br!=null){
try{
br.close();
}catch(IOException ex){
}
}
}
return null;
}
static String ReadOneLine2(String path)
throws IOException
{
try(BufferedReader br= new BufferedReader(new FileReader(path))){
return br.readLine();
}
}
}
public class TestTryFinally {
public static String output = "";
public static void foo(int i) {
try {
if (i == 1) {
throw new Exception();
}
output += "1";
} catch(Exception e) {
output += "2";
return;
} finally {
output += "3"; // 不论是否出现异常都会执行。
}
output += "4";
}
public static void main(String args[]) {
foo(0);
System.out.print(output + " ");
// foo(1);
// System.out.println(output);
}
}
import java.io.*;
public class ExceptionTrowsToOther{
public static void main(String[] args){
try{
System.out.println("====Before====");
readFile();
System.out.println("====After====");
}catch(IOException e){ System.out.println(e); }
}
/**
* 受检的异常:
* 要么捕(catch)
* 要么抛(throws): 在方法的签名后面用throws ***来声明
* 在子类中, 如果要覆盖父类的一个方法, 若父类的方法中方法声明了throws异常,则子类的方法也可以throws异常
* 可以抛出子类异常(更具体的异常),但不能抛出更一般的异常
*/
public static void readFile()throws IOException {
FileInputStream in=new FileInputStream("myfile.txt");
int b;
b = in.read();
while(b!= -1) {
System.out.print((char)b);
b = in.read();
}
in.close();
}
}
import java.io.*;
public class ExceptionForNum
{
public static void main(String[] args)
{
try{
BufferedReader in = new BufferedReader(
new InputStreamReader( System.in ) );
System.out.print("Please input a number: ");
String s = in.readLine();
int n = Integer.parseInt( s );
}catch(IOException ex){
ex.printStackTrace();
}catch(NumberFormatException ex){
ex.printStackTrace();
}
}
}
public class ExceptionCause {
/**
* 基本写法:
* try {
* 语句组;
* } catch (Exception ex) {
* 异常处理语句组;
* }
*
*/
public static void main(String [] args) {
try
{
BankATM.GetBalanceInfo( 12345L);
}catch(Exception e) { // 捕获异常代码
System.out.println("something wrong " + e);
System.out.println("cause" + e.getCause());
}
}
}
/**
* 对于异常,不仅要进行捕获处理, 有时还需要将此异常进一步传递给调用者,
* 以便让调用者也能感受到这种异常。
*
* 1. 将当前捕获的异常再次抛出:
* throw e;
* 2. 重新生成一个新的异常并抛出:
* throw new Exception("some message");
* 3. 重新生成并抛出一个新的异常, 该异常中包含了当前异常的信息:
* throw new Exception("some message", e);
* 可用getCause()来得到内部异常
*/
class DataHouse {
public static void FindData( long ID)
throws DataHouseException
{
if( ID>0 && ID<1000)
System.out.println( "id: " + ID );
else
throw new DataHouseException("cannot find the id");
}
}
class BankATM{
public static void GetBalanceInfo( long ID)
throws MyAppException
{
try
{
DataHouse.FindData(ID);
}catch (DataHouseException e) {
throw new MyAppException("invalid id",e);
}
}
}
/**
* 创建用户自定义异常类时:
* 1. 继承自Exception类或某个子Exception类
* 2. 定义属性和方法, 或重载父类的方法
*/
class DataHouseException extends Exception {
public DataHouseException( String message ) {
super(message);
}
}
class MyAppException extends Exception {
public MyAppException (String message){
super (message);
}
public MyAppException (String message, Exception cause) {
super(message,cause);
}
}