异常处理
trycatchfinallythrowthrows
1.throw抛异常
作用
可以使用throw关键字 在指定方法中抛出指定得异常
使用格式
throw new XXXException(“异常产生得原因”)
注意事项
1throw关键字抛出指定得异常对象 我们就必须处理这个异常对象
1)throw关键字抛出得是runtimeException 或者 RuntimeException子类
这个时候我们可以不处理 JVM会处理
2).throw关键字抛出得是编译异异常(写代码得时候报错 ) 这个异常必须要处理
throws try-catch
2.throw关键字必须写在方法内部
3.throw关键字 后面new得对象必须是Exception或者Exception子类
案例:
public static void main01(String[] args) {
getElement(new int[]{1,2,3}, 4);
}
//1.NullPointerException 空异常
//2.ArrayIndexOutOfBoundsException 超出索引
public static int getElement(int [] arr,int index){
//两个if 校验工作
if (arr==null) {
throw new NullPointerException("数组是空的 哈哈哈");
}
if (index<0||index>arr.length-1) {
throw new ArrayIndexOutOfBoundsException("数组超出了索引");
}
int ele=arr[index];
return ele;
}
public static void main02(String[] args) {
//method(null);
//Objects 静态方法
Objects.requireNonNull(null,"你给我的是空得");
}
public static void method(Object obj){
if (obj==null) {
throw new NullPointerException("传递得对象是Null");
}
}
2.编译期间异常
如果有异常 不会编译成字节码文件 Java–class
处理方式两种
1.throws 抛给调用者 最终会给JVM 2.try–catch
throws
异常处理得一种方式 交给别人处理
作用:
当方法内部 抛出异常对象得时候 那么我们必须处理这个异常
可以使用throws关键字处理异常对象 会把异常对象抛出给方法得调用者处理
使用格式:
修饰符 返回值类型 方法名(参数列表) throws AAAException,BBBException
{ throw new AAAException throw new BBBException }
注意事项:
1.throws关键字 必须写在方法声明处
2.throws关键字 后面声明得异常必须是Exception或者Exception的子类
3.方法内部如果抛出了多个异常对象 那么throws后面必须是多个异常
如果抛出得异常对象有父子关系 那么直接throws 父类就可以了
4.调用了一个声明抛出异常得方法 我们就必须处理声明异常
5.要么就继续使用throws抛出 交给方法得调用者处理 最终交给JVM 要么就使用try–catch
6.如果最终抛给JVM 1.打印抛出得异常信息 2.中断程序
案例:
public static void main03(String[] args) throws IOException {
readFile("d:\\a.txt");
System.out.println("没读完文件也没事");
}
public static void readFile1(String fileName) throws IOException {
//判断字符串filename 是可不是以txt结尾得
if (!fileName.equals("C:\\a.txt")) {
throw new FileNotFoundException("文件没有找到");
}
if (!fileName.endsWith(".txt")) {
throw new IOException("文件后缀名不对");
}
System.out.println("路径没问题,读取文件");
}
try-catch
第二种处理一次的方式
使用格式:
try{ 可能会产生得异常得代码 } catch(异常类型 变量名) {
}
注意事项:
1.try中可能会抛出多个异常对象 那么就可以使用多个Catch来处理这些异常对象
2.如果try中产生了异常 那么就会执行catch中得异常处理逻辑
3.执行完毕catch中处理逻辑 继续执行 try—catch之后得代码
4.如果try—catch中没有产生异常 那么就不会执行catch异常处理逻辑 执行try中得代码之后 继续执行try—catch之后代码
案例:
public static void main04(String[] args) {
try {
readFile("d:\\a.txt1");//可能会产生异常
}
catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("这次是后缀名");
}
catch (IOException e) {//捕获异常
// TODO Auto-generated catch block
// System.out.println("哈哈哈哈 有异常 但是我打印了没用得");
// e.printStackTrace();//JVM用的也是这个方法 异常内容 位置 信息
//Throwable 类中定义了3个异常处理得方法
//1.getMessage
System.out.println(e.getMessage());//文件没找打
//2.tostring() System.out.println(e.toString());//java.io.FileNotFoundException: 文件没有找到
//3.printStackTrace
e.printStackTrace();
}
finally {
System.out.println("后续代码来了");
int a=11+12;
}
}
public static void readFile(String fileName) throws IOException {
//判断字符串filename 是可不是以txt结尾得
//FileNotFoundException extends IOException
if (!fileName.equals("C:\\a.txt")) {
throw new FileNotFoundException("文件没有找到");
}
if (!fileName.endsWith(".txt")) {
throw new IOException("文件后缀名不对");
}
System.out.println("路径没问题,读取文件");
}
3.finally 代码块
格式
try{} catch(){} catch(){} ……………… finally{ 无论是否出现异常都会执行 }
注意事项:
1.finally不能单独使用 必须和try’一块使用
2.finally一般用于资源得释放 (资源回收) 无论程序是否出现异常 都要记得释放资源(IO流 数据库)
案例:
public static void main05(String[] args) {
try {
readFile("d:\\a.txt");//可能会产生异常
}
catch (FileNotFoundException e) {
// TODO: handle exception
System.out.println("这次是后缀名");
int [] arr={1,2};
System.out.println(arr[3]);
}
catch (IOException e) {//捕获异常
// TODO Auto-generated catch block
// System.out.println("哈哈哈哈 有异常 但是我打印了没用得");
// e.printStackTrace();//JVM用的也是这个方法 异常内容 位置 信息
//Throwable 类中定义了3个异常处理得方法
//1.getMessage
System.out.println(e.getMessage());//文件没找打
//2.tostring() System.out.println(e.toString());//java.io.FileNotFoundException: 文件没有找到
//3.printStackTrace
e.printStackTrace();
}
finally {
System.out.println("资源关闭");
}
System.out.println("后续代码来了");
int a=11+12;
}
}
4.自定义异常类
Java提供得异常类 不够我们使用 需要自定义一些异常类
格式:
public class XXXException extends Exception|| RuntimeException
{
一个无参数得构造函数
一个带异常信息得构造函数
}
案例:
public class RegisterException extends RuntimeException {
public RegisterException(){
super();
}
public RegisterException(String message){
super(message);//参数给了父类
}
}
异常:注册用户 如果用户名重复 提示 该用户名已经存在
案例:
public class ExceptionTest4 {
public static void main(String[] args) {
String st="我爱JAVA";
if ("我爱JAVA"==st) {
throw new RegisterException("注册失败");
}
else {
System.out.println("注册成功");
}
}
}
5.父子类之间的异常
父类方法没有抛出异常 子类重写该方法得时候 也不可以抛出异常
如果子类仍然产生了异常 只能try-catch
案例:
public class Fu {
public void show01() throws NullPointerException,ClassCastException{
}
public void show02() throws IndexOutOfBoundsException
{
}
public void show03() throws IndexOutOfBoundsException{
}
public void show4() throws Exception{
}
public void show5() {
}
}
public class Zi extends Fu {
@Override
public void show01() throws NullPointerException, ClassCastException {
// TODO Auto-generated method stub
super.show01();
}
@Override
public void show4() throws IOException {
// TODO Auto-generated method stub
}
@Override
public void show02() {
// TODO Auto-generated method stub
super.show02();
}
@Override
public void show5() {
// TODO Auto-generated method stub
super.show5();
}
}
总结:
父类异常什么样子 子类就应该怎么样
6.多个异常使用捕获
1.多个异常分别处理
2.多个异常 一次捕获 多次处理
3.多个一次捕获 一次处理
案例:
public static void main01(String[] args) {
SimpleDateFormat dateFormat=new SimpleDateFormat();
try {
int [] arr={1,2};
System.out.println(arr[3]);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
try {
dateFormat.parse("1999-10-10");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main03(String[] args) {
//如果两个异常 有父子关系 子类方法上面
SimpleDateFormat dateFormat=new SimpleDateFormat();
try {
int [] arr={1,2};
System.out.println(arr[0]);
dateFormat.parse("1999-10-10");
}
catch(ParseException e){
e.printStackTrace();
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main05(String[] args) {
SimpleDateFormat dateFormat=new SimpleDateFormat();
try {
int [] arr={1,2};
System.out.println(arr[0]);
dateFormat.parse("1999-10-10");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//finally 代码块块
//如果finally中有return语句 永远返回finally中得结果
//尽量不要在finally中写return
public static void main(String [] args){
System.out.println(getA());
}
public static int getA(){
int a=10;
try {
return a;
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
finally {
a=1000;
return a;
}
}
}