栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java异常

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java异常

 

 

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();//使用这个还是显示异常,不过程序还会往下执行
    }
    }

}

 

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();
    }
    }

}

 

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();
        }

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/874480.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号