Java异常

Java异常,第1张

 

 

public class Exception {
    public static void main(String[] args) {

        System.out.println("开始");
        method();
        System.out.println("结束");
    }
    public static void method(){
    try{int[] arr={1,2,3};
        System.out.println(arr[3]);
       // System.out.println(arr[3]);
//new ArrayIndexOutOfBoundsException();
    }catch (ArrayIndexOutOfBoundsException e){
     //   System.out.println("你访问的数组索引不存在");
e.printStackTrace();//使用这个还是显示异常,不过程序还会往下执行
    }
    }

}
/*
开始
        Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at Exception.method(Exception.java:10)
        at Exception.main(Exception.java:5)
*/

 

public class Exception {
    public static void main(String[] args) {

        System.out.println("开始");
        method();
        System.out.println("结束");
    }
    public static void method(){
    try{int[] arr={1,2,3};
        System.out.println(arr[3]);
       // System.out.println(arr[3]);
//new ArrayIndexOutOfBoundsException();
    }catch (ArrayIndexOutOfBoundsException e){
     //   System.out.println("你访问的数组索引不存在");
//使用这个还是显示异常,不过程序还会往下执行
        System.out.println(e.toString());
        e.printStackTrace();
    }
    }

}
/*
开始
        Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
        at Exception.method(Exception.java:10)
        at Exception.main(Exception.java:5)
*/

 

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

public class Exception {
    public static void main(String[] args) {
//method();
        method2();
    }
public static void method2(){
       try{
           String s="2048-08-09";
           SimpleDateFormat sdf=new SimpleDateFormat();
           Date d=sdf.parse(s);
           System.out.println(d);
       }catch (ParseException e){
           e.printStackTrace();
       }

}
    public   static void method() {
        int[] arr={1,2,3};
        System.out.println(arr[3]);
    }
}

 

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

public class Exception {
    public static void main(String[] args) {
        System.out.println("开始");
     method();
        System.out.println("结束");
    }
    public static void method()throws ArrayIndexOutOfBoundsException{
        int[] arr={1,2,3};
        System.out.println(arr[3]);
    }

}

 自定义异常

 

 

package heima;

public class SoreException extends Exception{
    public SoreException(){}
    public SoreException(String message){
    super(message);
    }

}
package heima;

public class Teacher {
public void checkScore(int score) throws SoreException{
    if(score<0||score>100){
        throw new SoreException("你给的分数有误");
    }else{
        System.out.println("分数正常");
    }
}

}
package heima;

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 (SoreException e) {
            e.printStackTrace();
        }

    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/905953.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-15
下一篇2022-05-15

发表评论

登录后才能评论

评论列表(0条)

    保存