Java基础之异常

Java基础

异常

int b = 0;
int r = 3/b;
异常就是一种错误,只是java提供了一个类解决这个错误。
Throwable------所有异常的父类
Error------Throwable子类:代码不能处理的 XXXError
Exception--------Throwable子类:代码能处理的 XXXException
RuntimeException--------Exception子类:运行时异常
IOExceptionFileNotFoundExceptionInterruptedException等等-------Exception子类:编译时异常

	package com.java.day11;
	
	import java.io.FileNotFoundException;
	import java.io.FileReader;
	
	public class Demo3 {
		
		//编译时异常
		public static void test2() throws FileNotFoundException {
			FileReader fr = new FileReader("a.txt");
		}
		
		//处理异常的两种方式
		//1.throws抛出异常(声明异常)	半成品异常处理方式	手榴弹
		//2.try catch捕获异常	直接处理掉
		//异常代码块:在日志文件中保存堆栈信息
		public static void test3() {
			try {
				//try块中一条语句一旦发生异常,后面语句不会执行
				FileReader fr = new FileReader("a.txt");
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();//打印堆栈信息
			}finally {
				//不管有没有异常都会执行该块
			}
		}
		
		//运行时异常
		//运行时异常,如果代码写的严谨一些,可以避免运行时异常
		public static void test1() {
			String[] names = null;
			System.out.println(names[0].substring(0));
			int[] num = {2};
			System.out.println(num[1]);
		}
		
		//try后边必须有catch块或finally块,一个try后边可以有多个catch块
		//try-finally语法是正确的,但不能捕获异常
		//try-catch块可以嵌套使用
		public static void test4() {
			try {
				FileReader fr = new FileReader("a.txt");
				Thread.sleep(1000);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		public static void main(String[] args) {
			//test1();
			//test2();
			//test3();
			test4();
		}
	
	}

try-catch 练习

package com.java.day11;

public class Demo4 {
	
	public static int test1() {
		try {
			return 1;
		}catch(Exception e) {
			return 2;
		}finally {
			System.out.println("end");
		}
	}
	
	public static int test2() {
		int i = 0;
		try {
			return i++;
		}catch(Exception e) {
			return 2;
		}finally {
			i++;
			System.out.println(i);
			System.out.println("end");
		}
	}
	
	public static int test3() {
		int i = 0;
		try {
			return i++;
		}catch(Exception e) {
			return 2;
		}finally {
			i++;
			System.out.println("end");
			return i;
		}
	}
	
	public static void main(String[] args) {
		//System.out.println(test1());//end 1
		//System.out.println(test2());//2 end 0
		System.out.println(test3());//end 2
	}

}

注意
在finally块中尽量不要写return,有时候会屏蔽异常代码。
如:

	public static String test4(String name) {
		try {
			name.length();
		}finally {
			return name+"hello";
		}
	}
	public static void main(String[] args) {
		System.out.println(test4(null));//nullhello
	}

自定义异常类
必须要继承异常类
public interface Message{
String ageMessage = “请输入正确范围的数字(1-150)”;
}
//编译时异常
public class AgeException extends Exception{
public AgeException(){
//完成调用父类带参的构造方法,初始化错误信息
super(Message.ageMessage);
}
}
使用自定义异常
if (age<1||age>150)
throw new AgeException();//抛出异常

	package com.java.day11_2;
	//异常信息接口,便于修改异常信息
	public interface ExceptionMessage {
		String ageExceptionMessage = "年龄设置超出范围(1-150)";
	}

	package com.java.day11_2;
	//自定义异常类
	public class AgeException extends Exception{
		public AgeException() {
			super(ExceptionMessage.ageExceptionMessage);
		}
	}

	package com.java.day11_2;
	//使用自定义异常
	public class Student {
		private int age;
	
		public int getAge() {
			return age;
		}
	
		public void setAge(int age) throws AgeException {
			if(age < 1 || age > 150)
				throw new AgeException();
			this.age = age;
		}
		
	}

	package com.java.day11_2;
	//测试类
	public class Test {
	
		public static void main(String[] args) {
			Student stu = new Student();
			try {
				stu.setAge(5);
				System.out.println(stu.getAge());
			} catch (AgeException e) {
				System.out.println(e.getMessage());
			}
		}
	
	}

方法重写时异常规则
子类异常<=父类的异常(指编译时异常,和运行时异常没有关系)
<=:个数,继承关系,
throw和throws的区别

throwthrows
抛出异常声明异常
方法内方法声明
+异常类的对象+异常类的类型
只能跟一个异常类的对象可以跟多个异常类的类型 用,隔开
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值