赞
踩
这里是integer处理的值应该是整数,但是这里是字符串,这就引起报错,valueof方法内部会封装异常对象然后抛出main方法交给JVM处理
JVM会把这个异常对象打印处理 这个对象的信息包括错误的原因和行号
Error:代表的系统级别错误(属于严重问题),也就是说系统一旦出现问题,sun公司会把这些问题封装成Error对象给出来
说白了,Error是给sun公司自己用的,不是给我们程序员用的,因此我们开发人员不用管它。
Exception:叫异常,它代表的才是我们程序可能出现的问题,所以,我们程序员通常会用Exception以及它的孩子来封装程序出现的问题。
数组索引越界异常
public class ExceptionTest {
public static void main(String[] args) {
int[] arr ={11,22,33};
System.out.println(arr[5]);
}
}
日期解析异常
这里parse会触发检查错误,不处理不能执行
使用try…catch
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionTest {
public static void main(String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=sdf.parse( "2028-11-11 10:24");
System.out.println(d);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
}
或者使用抛出异常的方式 throws ParseException
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionTest {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d=sdf.parse( "2028-11-11 10:24");
System.out.println(d);
}
}
Java无法为这个世界上全部的问题都提供异常类来代表,如果企业自己的某种问题,想通过异常来表示
以便用异常来管理该问题,那就需要自己来定义异常类了。
这段代码上层调用saveAge(223);,不会知道这个方法到底执行成功没有,因为这个方法没有返回值,如果要返回执行情况,要用一个异常对象封装这个问题
public class ExceptionTest {
public static void main(String[] args) {
// 需求:保存一个合法的年龄
saveAge(223);
}
public static void saveAge(int age) {
if (age > 0 && age < 150) {
System.out.println("年龄被成功保存:" + age);
} else {
System.out.println("年龄非法!:");
}
}
}
异常体系的两大阵营
自定义体系的两大阵营
第一种:自定义运行时异常
用一个异常对象封装这个问题,throw 抛出去这个异常对象 抛出的异常使用try...catch
来捕获
public class ExceptionTest {
public static void main(String[] args) {
// 需求:保存一个合法的年龄
try {
saveAge(15);
System.out.println("底层执行成功的!");//没有捕获到异常就是成功
} catch (Exception e) {
e.printStackTrace();
System.out.println("底层出现了bug!");//捕获到异常就是提示
}
}
public static void saveAge(int age) {
if (age > 0 && age < 150) {
System.out.println("年龄被成功保存:" + age);
} else {
// 用一个异常对象封装这个问题
// throw 抛出去这个异常对象 抛出的异常使用try...catch来捕获
throw new AgeIllegalRuntimeException("/age is illegal, your age is" + age);
}
}
}
package com.qianxin.yichang;
public class AgeIllegalRuntimeException extends RuntimeException{
public AgeIllegalRuntimeException() {
}
public AgeIllegalRuntimeException(String message) {
super(message);
}
}
第二种:自定义编译时异常 问题严重时可以使用这个
public class ExceptionTest {
public static void main(String[] args) {
// 需求:保存一个合法的年龄
try {
saveAge(155);
System.out.println("底层执行是成功的!");//没有捕获到异常就是成功
} catch (Exception e) {
e.printStackTrace();
System.out.println("底层出现了bug!");//捕获到异常就是提示
}
}
public static void saveAge(int age) throws AgeIllegalException {
if (age > 0 && age < 150) {
System.out.println("年龄被成功保存:" + age);
} else {
// 用一个异常对象封装这个问题
// throw 抛出去这个异常对象 抛出的异常使用try...catch来捕获
// throws 用在方法上,抛出方法内部的异常
throw new AgeIllegalException("/age is illegal, your age is" + age);
}
}
}
实际开发中这两个异常的基本内容大致相同,那我们应该用哪个呢?
三个方法依次调用,C出现异常的话会抛给B,B再抛给A ,此时A不能再抛出去
,应该使用异常捕获来抓取错误信息,然后优化显示给用户,提升使用感
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 ExceptionTest {
public static void main(String[] args) {
try {
test1();
} catch(FileNotFoundException e){
System.out.println("您要找的文件不存在!!");
e.printStackTrace();//打印出这个异常对象的信息。记录下来。
}catch(ParseException e) {
System.out.println("您要解析的时间有问题了!");
e.printStackTrace();//打印出这个异常对象的信息。记录下来
}
}
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:24:11");
System.out.println(d);
test2();
}
public static void test2() throws FileNotFoundException{// 读取文件的。
InputStream is = new FileInputStream("D:/meinv.png");
}
}
代码优化 使用Exception处理一切异常 而不写异常具体是什么异常
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionTest {
public static void main(String[] args) {
try {
test1();
} catch(Exception e) {
System.out.println("您的操作有问题!!");
e.printStackTrace();//打印出这个异常对象的信息。记录下来。
}
}
public static void test1()throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date d = sdf.parse("2028-11-11 10:24:11");
System.out.println(d);
test2();
}
public static void test2() throws Exception{// 读取文件的。
InputStream is = new FileInputStream("D:/meinv.png");
}
}
2、捕获异常,尝试重新修复
import java.util.Scanner;
public class ExceptionTest {
public static void main(String[] args) {
// 需求:调用一个方法,让用户输入一个合适的价格返回为止。// 尝试修复
while (true) {
try {
System.out.println(getMoney());
break;
} catch (Exception e) {
System.out.println("请您输入合法的数字!!");
}
}
}
public static double getMoney() {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入合适的价格:");
double money = sc.nextDouble();
if (money >= 0) {
return money;
} else {
System.out.println("您输入的价格是不合适的!");
}
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。