您的位置:首页 > 娱乐 > 八卦 > java-异常

java-异常

2024/10/6 8:34:34 来源:https://blog.csdn.net/2301_80708315/article/details/141753676  浏览:    关键词:java-异常

认识异常 

1.运行时异常

下面这行代码是将字符串转化为数字,而输入abc会报错 

Integer.valueOf("abc");

数组越界异常 

  int [] arr ={11,22,33};System.out.println(arr[5]);

2.编译时异常

当你打出下面这行代码时,系统会爆红 

提醒你要检查日期格式

 SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");Date d= sdf.parse("2028-11-11 10:28");

 

 解决方法

1.利用Ctrl+Alt+T快捷键实现try+catch 

 

 

2.throw  ParseException 

 

 

 

 自定义异常

 

1.自定义运行时异常 

//1.必须让这个类继承RuntimeException才能成为一个运行异常类
public class Test7_2AgelllegalRuntimeException extends RuntimeException{public Test7_2AgelllegalRuntimeException() {}public Test7_2AgelllegalRuntimeException(String message) {super(message);}
}

重写构造器时选择前两个 

 

else {//用一个异常对象封装这个问题//throw抛出这个异常问题throw  new Test7_2AgelllegalRuntimeException("/age is illegal age is "+age);}

 完整代码

//自定义异常
public class Test7_1exception {public static void main(String[] args) {//保存一个合法年龄try {saveAge(160);System.out.println("底层执行成功");} catch (Exception e) {e.printStackTrace();//打印异常System.out.println("底层出现问题");}}public  static  void  saveAge(int age){if(age>0&&age<150){System.out.println("年龄被成功保存"+age);}else {//用一个异常对象封装这个问题//throw抛出这个异常问题throw  new Test7_2AgelllegalRuntimeException("/age is illegal age is "+age);}}
}

 

当你输入223时会出现异常 

 

2.自定义编译时异常 

1,必须让这个类继承自Expection,才能成为一个异常类 

//1,必须让这个类继承自Expection,才能成为一个异常类
public class Test7_3AgelllegalException  extends Exception{public Test7_3AgelllegalException() {}public Test7_3AgelllegalException(String message) {super(message);}
}

写出来就会爆红,提醒  

 

解决方法

2.在方法外写上throws+异常名 

注意: 

throw 是抛出这个异常对象

throws是用在方法上,抛出方法内部异常 

 

3.再在主函数调用此方法时再利用try+catch

   try {saveAge2(25);} catch (Test7_3AgelllegalException e) {e.printStackTrace();}

同样的在输入225时会报错 

 

 

异常的处理 

 

 1.捕获异常,记录异常并响应合适的信息给用户

先将test的问题抛出 

public  static void  test2() throws FileNotFoundException {//读取文件InputStream is =new FileInputStream("D//meinv.nnn");}

其中test1中调用了test2()再将两者的信息抛出 

 public  static  void test1() throws FileNotFoundException, ParseException {SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");Date d= sdf.parse("2028-11-11 10:28");System.out.println(d);test2();}

最后再利用try+catch将问题抛出

 

public class Test7_4expection {public static void main(String[] args) {try {test1();} catch (FileNotFoundException e) {System.out.println("您要找的文件不存在");e.printStackTrace();//打印异常信息,记录异常信息} catch (ParseException e) {System.out.println("您解析的时间有问题");e.printStackTrace();//打印异常信息,记录下来}}

 改进版:将所有的问题用Expection抛出

  public static void main(String[] args) {try {test1();} catch (Exception e) {System.out.println("您当前的操作有问题");e.printStackTrace();//打印异常信息,记录异常信息}}public  static  void test1() throws Exception,Exception {SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");Date d= sdf.parse("2028-11-11 10:28");System.out.println(d);test2();}public  static void  test2() throws Exception {//读取文件InputStream is =new FileInputStream("D//meinv.nnn");}
}

2.捕获异常,尝试重新修复 

 

 用户乱输入导致异常 

 

 

package com.itcollection;import java.util.Scanner;public class Test5_5expection {public static void main(String[] args) {//需求:调用一个方法,让用户输入一个合适的价格返回为止while (true) {try {System.out.println(getMoney());break;} catch (Exception e) {System.out.println("请您输入合适的数字");}}}public  static  double getMoney(){while (true) {System.out.println("请输入价格");Scanner sc =new Scanner(System.in);Double d = sc.nextDouble();if(d>=0){System.out.println("您输入的价格合适");return d;}else {System.out.println("您输入的价格不对");}}}
}

 

 

 

 

 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com