import java.util.Scanner;
public class ArrayIndex {
public static void main(String[] args) {
int[] a =new int[10];
int idx;
Scanner in = new Scanner(System.in);
idx = in.nextInt();
try {
a[idx]=10;
System.out.println("hello");
}catch(ArrayIndexOutOfBoundsException e){
//try{}中内容,遇到错误为()时,执行以下内容
//ArrayIndexOutOfBoundsException:索引超过设定值9
System.out.println("Caught");
}
}
}
try {
//可能产生异常的代码
}catch(Type1 id1) {
//处理Type1异常的代码
}catch(Type2 id2) {
//处理Type2异常的代码
}
public class ArrayIndex {
public static void f() {
int[] a =new int[10];
a[10]=10;
}
public static void g() {
f();
}
public static void k() {
try {
g();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("k()");
throw e;
}
}
public static void main(String[] args) {
try {
k();
System.out.println("hello");
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Caught");
System.out.println(e.getMessage());
System.out.println(e);
e.printStackTrace();
}
}
}
输出:
k()
Caught
10
java.lang.ArrayIndexOutOfBoundsException: 10
java.lang.ArrayIndexOutOfBoundsException: 10
at test_8.ArrayIndex.f(ArrayIndex.java:6)
at test_8.ArrayIndex.g(ArrayIndex.java:9)
at test_8.ArrayIndex.k(ArrayIndex.java:13)
at test_8.ArrayIndex.main(ArrayIndex.java:22)
class OpenException extends Throwable{
}
class CloseException extends Throwable{
}
public class ArrayIndex {
public static int open() {
return -1; //如果文件打开失败返回-1
}
public static void readFile() throws OpenException,CloseException { //声明有可能抛出异常
if(open()==-1) {
throw new OpenException();
}
}
public static void main(String[] args) {
try {
readFile(); //函数有可能抛出异常,需要用try/catch结构
} catch (OpenException e) {
e.printStackTrace();
} catch (CloseException e) {
e.printStackTrace();
}
}
}
catch(Exception e){ //捕捉任何异常,万能捕捉器
e.printStackTrace();
System.out.println("Anything");
}
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("读到了"+len+"字节");
System.out.println(s);
System.out.println("s的长度是"+s.length());
} catch (IOException e) {
e.printStackTrace();
}
}
}
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;
FileOutputStream out;
try {
out = new FileOutputStream("a.dat");
out.write(buffer);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DataOutputStream out =new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream("a.dat")));
int i = 112233;
out.writeInt(i);
out.close();
DataInputStream in = new DataInputStream(
new BufferedInputStream(
new FileInputStream("a.dat")));
int j = in.readInt();
System.out.println(j);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("a.txt"))));
int i = 112233;
out.println(i);
out.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream("a.txt")));
String line;
while((line=in.readLine())!=null) {
System.out.println(line);
// in.readLine() 返回一整行String,如果读到末尾返回Null
}
new InputStreamReader(new FileInputStream("a.txt"),"utf8")
public class Main{
public static void main(String[] args) {
try {
Socket socket = new Socket(InetAddress.getByName("localhost"),12345);
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream()))); // 得到虚拟的流,我与服务器之间的网络连接,构建了一个writer,可以送东西过去
out.println("hello");
out.flush();
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String line;
line = in.readLine(); //停在这里等待接收信息
System.out.println(line);
out.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
nc -l -p 12345
setSoTimeout(int timeOut)
class Student implements Serializable{ //可串行化的类
private String name;
private int age;
private int grade;
public Student(String name,int age,int grade) {
this.name=name;
this.age=age;
this.grade=grade;
}
public String toString() {
return name+" "+age+" "+grade;
}
}
public class Main{
public static void main(String[] args) {
try {
Student s1 = new Student("John", 18, 5);
System.out.println(s1);
ObjectOutputStream out = new ObjectOutputStream( //将s1写入文件里
new FileOutputStream("obj.dat"));
out.writeObject(s1);
out.close();
ObjectInputStream in = new ObjectInputStream(
new FileInputStream("obj.dat"));
Student s2 = (Student)in.readObject();
System.out.println(s2);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}