捕捉异常
try {
//可能异常的代码
} catch(Type id1) {
//处理Type1异常的代码
} catch(Type id2) {
//处理Type2异常的代码
} catch(Type id3) {
//处理Type3异常的代码
}
捉到异常做什么?
String getMessage();
String toString();
void printStackTrace();
但是无法再回去,具体利用逻辑取决于业务逻辑。
再度抛出
catch (Exception e) {
System.out.println("An Exception was thrown");
throw e;
}
异常
抛出异常
throw
什么能扔?
- 任何继承了Throwable类的对象
- Exception类继承了Throwable
- throw new Exception();
- throw new Exception(“Help”);
catch怎么匹配异常的?
捕捉任何异常
catch (Exception e) {
...
}
运行时刻异常,像ArrayIndexOutOfBoundsException
这样的异常是不需要声明的,但是如果没有适当的机制来捕捉,会导致程序终止。
在Java匿名类中抛出异常怎么做?
匿名类中抛出异常
异常遇到继承
如果调用一个 声明会抛出异常的函数 ,那么你必须:1.把函数的调用放在try块中,并设置catch来捕捉所有可能抛出的异常。或 2.声明自己会抛出无法处理的异常。
流(Stream)
一维的,单方向的
输入流
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World~");
byte[] buffer = new byte[1024];
try {
int len = System.in.read(buffer);
String s = new String(buffer, 0, len);
System.out.println("Get " + len + " Byte");
System.out.println(s);
System.out.println("lenth of s: " + s.length());
} catch (IOException e) {
System.out.println("Caught!");
}
}
}
文件流
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World~");
byte[] buffer = new byte[10];
for(int i = 0; i<buffer.length; i++)
{
buffer[i] = (byte)i;
}
try {
FileOutputStream out = new FileOutputStream("out.dat");
out.write(buffer);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
流过滤器
二进制数据采用 InPutStream / OutPutStream
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World~");
// byte[] buffer = new byte[10];
// for(int i = 0; i<buffer.length; i++)
// {
// buffer[i] = (byte)i;
// }
try {
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("out.dat"))
);
int i = 123456;
out.writeInt(i);
out.close();
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream("out.dat"))
);
int j = in.readInt();
System.out.println(j);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
文本流
文本数据采用 Reader / Writer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World~");
try {
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("C:\\Users\\16922\\Desktop\\out.txt")))
);
int i = 123456;
out.println(i);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream("C:\\JavaCode\\Main\\src\\hello\\Main.java"))
);
String line;
while( (line = in.readLine()) != null )
System.out.println(line);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
LineNumberReader
- 可以得到行号 getLineNumber();
FileReader