异常

异常


异常的处理

如果程序出现了异常,我们就要自己处理,有两种方法

  1. try...catch...
  2. throws

/*格式:
    try{可能出现的异常代码}catch(异常类名 变量名)
    {异常处理的代码}
    常用方法:
    1. public String getMessage():返回异常原因
    2. public String toString():返回异常的类名和原因
    3. publci void printStackTrace():返回异常的类名,原因和位置.
 */
public class ExceptionDemo {
    public static void main(String[] args) {
        System.out.println("Start");
        method();
        System.out.println("End");
    }
    static void method(){
        try {           //和java处理方式一样,但是程序可以跳过异常继续执行.
            int arr[] = {1, 2, 3};
            System.out.println(arr[3]);
        }catch(ArrayIndexOutOfBoundsException a){
            //System.out.println("索引越界!");
            /*Start
               索引越界!
               End
             */
            a.printStackTrace();//Java默认处理方式
        }
    }
}
/*
Start
End
java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at ExceptionDemo.method(ExceptionDemo.java:14)
	at ExceptionDemo.main(ExceptionDemo.java:8)
 */

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ExceptionDemo1 {
    public static void main(String[] args) {
        try {
            String s = "2023-02-15";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = sdf.parse(s);//异常信息:Unhandled exception: java.text.ParseException
            System.out.println(parse);
        }catch(ParseException e){   //异常类名  变量名
            e.printStackTrace();
        }
    }
}

//  throws 一般处理编译时异常
public class ExceptionDemo1 {
    public static void main(String[] args) {
           a(); //还需要try...catch...去处理
    }
    void a () throws ParseException {  //抛出异常,让调用者去处理
        String s = "2023-02-15";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse(s);//异常信息:Unhandled exception: java.text.ParseException
        System.out.println(parse);
    }
}

自定义异常

为了满足自定义的需求比如判断学生的成绩,我们就要自己来写一个异常类.

package CustomException;
//自定义异常类    继承于 Exception类 无参,带参构造方法
public class scoreException extends Exception {
        public scoreException(){}
    public scoreException(String message){
            super(message);
    }
}//把message传给父类
//-----------------------------------------------
package CustomException;
public class teacher {//方法名后面用throws,方法中用throw
    public void checkScore(int score)throws scoreException{
        if(score<0||score>100){//在方法中new异常类,后面的信息是参数(message)
            throw new scoreException("分数有误(分数应在0~100之间).");
        }else System.out.println("分数正常.");
    }
}
//-----------------------------------------------
package CustomException;
import java.util.Scanner;
public class teacherTest {//抛出异常
    public static void main(String[] args) throws scoreException {
        teacher t = new teacher();
        Scanner s = new Scanner(System.in);
        System.out.println("输入分数:");
        int i = s.nextInt();
        t.checkScore(i);
    }
}
/*  控制台输出:
输入分数:
120
Exception in thread "main" CustomException.scoreException: 分数有误(分数应在0~100之间).
	at CustomException.teacher.checkScore(teacher.java:5)
	at CustomException.teacherTest.main(teacherTest.java:9)
*/

posted @ 2023-02-15 11:30  大宝贝94106  阅读(53)  评论(0)    收藏  举报