package com.day33.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
public class ExceptionTest {
public static void main(String[] strings) {
try {
method2();
} catch (IOException e) {
System.out.println(e.getMessage());
}
Student student =new Student();
try {
student.regist(-100);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static void method1() throws FileNotFoundException,IOException {
File file =new File("hello.txt");
FileInputStream fileInputStream =new FileInputStream(file);
int data=fileInputStream.read();
while (data != -1) {
System.out.println((char) data);
data=fileInputStream.read();
}
fileInputStream.close();
}
public static void method2() throws IOException {
method1();
}
}
class Student{
private int id;
public void regist(int id) throws Exception {
if(id>0) {
this.id=id ;
}else {
throw new MyException("不支持负数");
}
}
}
package com.day33.test;
public class MyException extends Exception {
static final long serialVersionUID = -3387516993124229948L;
public MyException() {
}
public MyException(String str) {
super(str);
}
}