异常
package yichang;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Demo1 {
public static void main(String[] args) {
System.out.println("开始");
method0();
method1();
try {
method2();
}catch (ParseException e){
e.printStackTrace();
}
try {
method3();
}catch (ArrayIndexOutOfBoundsException e){
e.printStackTrace();
}
System.out.println("结束");
}
public static void method0(){
try{
String s="2020-08-24";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date d=sdf.parse(s);
System.out.println(d);
}catch (ParseException e){
e.printStackTrace();
}
}
public static void method1(){
try {
int[] arr={1,2,3};
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e){
System.out.println(e.getMessage());
System.out.println(e.toString());
e.printStackTrace();
}
}
public static void method2() throws ParseException{
String s="2020-08-24";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date d=sdf.parse(s);
System.out.println(d);
}
public static void method3() throws ArrayIndexOutOfBoundsException {
int[] arr = {1, 2, 3};
System.out.println(arr[3]);
}
}
package yichang;
public class ScoreException extends Exception{
public ScoreException(){}
public ScoreException(String message){
super(message);
}
}
package yichang;
public class Teacher {
public void checkScore(int score) throws ScoreException{
if(score<0 || score>100){
throw new ScoreException("你给的分数有误,分数应该在0-100之间");
}else{
System.out.println("分数正常");
}
}
}
package yichang;
import java.util.Scanner;
public class TeacherTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入分数:");
int score=sc.nextInt();
Teacher t=new Teacher();
try {
t.checkScore(score);
} catch (ScoreException e) {
e.printStackTrace();
}
}
}