网站建设价格标准报价单,门户网站建设先进性,动态广告图片在线制作,wordpress 访问量过大异常
很重要#xff0c;有利于我们平时处理问题 异常就是代表程序出现了问题 常见的异常比如说
数组越界除法除0
异常的体系是什么
java.lang.Throwable Error Exception RuntimeException 其他异常
Error 代表的是系统级别的错误#xff0c;也就是一旦系统出现问题有利于我们平时处理问题 异常就是代表程序出现了问题 常见的异常比如说
数组越界除法除0
异常的体系是什么
java.lang.Throwable Error Exception RuntimeException 其他异常
Error 代表的是系统级别的错误也就是一旦系统出现问题sun公司会把这些问题封装程Error对象出来 Error 是sun公司自己用的不是给我们程序员用的我们开发人员不用管 Exception叫异常它代表的才是我们程序可能出现的问题所以我们程序员通常会用 Exception 以及它的孩子来封装程序出现的问题。 运行时异常RuntimeException及其子类编译阶段不会出现错误提醒运行时出现的异常如数组索引越界异常 编译时异常编译阶段就会出现错误提醒的。如日期解析异常 抓住异常我们通过这个代码来进行抓住异常如果 try 里面的代码是有异常的那我们就进行捕捉如果捕捉到我们就可以得到这个异常信息并输出这个异常的信息 try {SimpleDateFormat date new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);Date d date.parse(2020-11-5 11:50:90);System.out.println(d);} catch (ParseException e) {e.printStackTrace();}自定义运行时异常
定义一个异常类继承RuntimeExceptin重写构造器通过throw new 异常类来创建异常对象并输出
编译阶段不报错,提醒不强烈,运行时才报错
public class exception {public static void main(String[] args) {saveAge(180);}public static void saveAge(int age){if(age0age150){System.out.println(年龄被成功保存age);}else {throw new AgeIllegalRuntimeException(age is illegal ,your age is age);}}
}我们这个新建的这个异常类我们是继承这个RuntimeException
public class AgeIllegalRuntimeException extends RuntimeException{//构造函数public AgeIllegalRuntimeException() {}//构造函数,其中这个message是输出的异常信息public AgeIllegalRuntimeException(String message) {super(message);}
}抛出编译时异常
public class exception {public static void main(String[] args) {saveAge(12);//saveAge2(25); //这里会直接报错我们有两种解决办法一种是继续往上抛try {saveAge2(160);System.out.println(程序正常执行);} catch (AgeIllegleException e) {System.out.println(程序异常);throw new RuntimeException(e);}}public static void saveAge(int age){if(age0age150){System.out.println(年龄被成功保存age);}else {throw new AgeIllegalRuntimeException(age is illegal ,your age is age);}}//throws 是在这个方法中抛出异常让该方法调用的时候出现//throw 跑出去一个异常对象。public static void saveAge2(int age) throws AgeIllegleException {if(age0age150){System.out.println(年龄被成功保存age);}else {throw new AgeIllegleException(age is illegal ,your age is age);}}
}分析上面的代码,我们可以看到这些异常的处理的时候我们发现我们可以不断的向外面抛出异常,但是一直抛出异常肯定是不可以的,因此我们需要对其进行捕获异常,利用try catch,进行记录异常,并记录处理信息
异常处理
1. 捕获异常,记录异常并相应合适的信息给用户
2. 捕获异常,尝试重新修复1. 捕获异常,记录异常并相应合适的信息给用户
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class ExceptionNesting {public static void main(String[] args) {try {test1();} catch (FileNotFoundException e) {System.out.println(文件未找到);throw new RuntimeException(e);} catch (ParseException e) {System.out.println(日期格式 不正确);throw new RuntimeException(e);}}public static void test1() throws FileNotFoundException, ParseException {SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);Date date sdf.parse(2020-10-20 12:12);System.out.println(date);test2();}public static void test2() throws FileNotFoundException {InputStream is new FileInputStream(D:/meinv.png);}
}2. 捕获异常并进行处理
import java.util.Scanner;public class StrongExceptionCorrect {public static void main(String[] args) {while (true) {try {System.out.println(getMoney());break;} catch (Exception e) {System.out.println(你输入的价格不合适请重新输入(你输入的并不是一个double类型的变量,可能输入了字符串导致异常));}}}public static double getMoney(){Scanner input new Scanner(System.in);while (true){System.out.println(请您输入合适的价格);double money input.nextInt();if(money0){return money;}else {System.out.println(你输入的价格是不合适的);}}}
}