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

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

package heima;

public class abc {
    public static void main(String[] args) {
        System.out.println("第一段代码");
        int i=10;
        String str=null;
int[] arr=new int[3];
try{
    System.out.println(i/0);
    System.out.println(str.length());
    arr[10]=100;
}catch (ArithmeticException e){
    System.out.println("除数为0");
}catch (NullPointerException e){
    System.out.println("空指针异常");
}catch (ArrayIndexOutOfBoundsException e){
    System.out.println("数组下标越界异常");
}
        System.out.println("第二段代码");


    }
}
package heima;

public class abc {
    public static void main(String[] args) {
        System.out.println("第一段代码");
        int i=10;
        String str=null;
int[] arr=new int[3];
try{
    System.out.println(i/0);
    System.out.println(str.length());
    arr[10]=100;
}
catch (ArithmeticException e){
    e.printStackTrace();
    System.out.println("除数为0");
}
catch (NullPointerException e){
    System.out.println("空指针异常");
}catch (ArrayIndexOutOfBoundsException e){
    System.out.println("数组下标越界异常");
}
catch (Exception e){
    e.printStackTrace();
}
        System.out.println("第二段代码");


    }
}

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;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class abc {
    public static void main(String[] args) {
     try{
         calc();
     }catch (Exception e){
         e.printStackTrace();
     }
        System.out.println("*********");

    }
 public static int calc()throws ArithmeticException,NullPointerException,Exception{
    int a=10;
    int b=0;
        int result=a/b;
        return result;
 }


}

自定义异常

 

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

    }
}

finally

package heima;

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class abc {
    public static void main(String[] args) {
    int a=10;
    int b=0;
    if(a==10)return;
//只要程序执行了try块中的代码,finally就都会执行 前提是没有执行System.exit();
    try{
        System.out.println(a/b);
    }catch (Exception e){
        System.out.println("除数不能为0");
    }finally {
        System.out.println("finally......");
    }
        System.out.println("**********");


     }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀化第一深情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值