学习这件事不在乎有没有人教你,最重要的是在于你自己有没有觉悟和恒心。——法布尔
一、异常简介
异常:程序出现意外导致中断指令流的一种事件。
异常是一个类,继承于Throwable类,其中包括2个主要大类:
Error(错误) 和 Exception(异常)。
Error:包括虚拟机错误和线程死锁,一旦Error出现了,程序就彻底的挂了,被称为程序终结者;
Exception:分为编译期异常和运行时异常(RuntimeException)
实践代码,搓搓小手动起来(=!=):
几个常见的异常:
public class ExceptionClass {
public static void main(String[] args) {
int a[] = new int[10];
//数组越界异常 java.lang.ArrayIndexOutOfBoundsException: 10
System.out.println(a[10]);
//空指针异常 java.lang.NullPointerException
//a=null;
//System.out.println(a.length);
//算数异常 java.lang.ArithmeticException: / by zero
//System.out.println(1/0);
//byte b=200;//编译期异常
//类转换时异常 java.lang.ClassCastException
//ExceptionClass t = (ExceptionClass)new Object();
//字符串的索引越界 java.lang.StringIndexOutOfBoundsException: String index out of range: 8
//String s = "hello";
//s.charAt(8);
}
}
输出结果:
二、异常处理
1.try-catch-finally语句
try: 用于监听可能抛出异常的代码
catch: 用于捕获try语句块中发生的异常
finally:finally语句块总是会被执行
try语句块不可以独立存在,必须与 catch 或者 finally 块同存
catch可以有多个
try{
可能出现问题的代码
}catch(Exception e){
出现问题后的解决方法
}finally{
一定会执行的代码,用于关闭和释放资源
}
实践代码,搓搓小手动起来(=!=):
public class DisposeClass {
public static void main(String[] args) {
// try-catch-finally
try {
//可能出现问题的代码
System.out.println(1 / 0);
} catch (Exception e) {
//对于这个问题的解决方案
System.out.println("除数不能为0");
}finally {
//程序一定执行的代码,除非虚拟机炸了
System.out.println("try-catch-finally");
}
}
}
2.throw和throws关键字
异常抛出(抛异常)通常使用throw和throws关键字来实现。
throws:声明将要抛出何种类型的异常(声明异常)。
当前异常在本方法中解决不了,抛出异常,谁调用谁处理。
实践代码,搓搓小手动起来(=!=):
public class DisposeClass {
public static void main(String[] args) {
//调用存在异常的方法
try {
fun();
} catch (Exception e){
System.out.println("算数异常");
e.printStackTrace();//打印异常信息
}
}
// throws
//可能存在异常问题的方法,谁调用谁处理
public static void fun() throws Exception{
System.out.println(1/0);
}
}
throw:将产生的异常抛出,是抛出异常的一个动作
public class ThrowClass {
public static void main(String[] args) {
Boolean a=true;
if (a) {
//如果a==ture,则抛出异常
throw new NullPointerException();
}
}
}
抛出的异常是可以自定义的
编译期异常,自定义异常类继承Exception,需要标明当前方法可能抛出的异常
运行期异常,自定义异常类继承RuntimeException,不需要标明 当前方法可能抛出的异常
实践代码,搓搓小手动起来(=!=):
public class Atm {
int money;
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
//存钱
public void add(int money) {
this.money+=money;
}
//取钱
public void reduce(int money) throws MoneyException, MoneyNotEnoughException {
if (money<0) {
//编译期异常,MoneyException继承Exception,需要标明当前方法可能抛出的异常
throw new MoneyException("抛出异常:钱不能为负数");
} else if (this.money<money) {
//运行期异常,MoneyNotEnoughException继承RuntimeException,不需要标明 当前方法可能抛出的异常
throw new MoneyNotEnoughException();
}
this.money-=money;
}
}
下面2个是自定义的异常类
public class MoneyException extends Exception{
//当继承Exception时,代表当前类为编译时异常,必须标明当前方法可能抛出的异常
public MoneyException(String str) {
super(str);
}
}
public class MoneyNotEnoughException extends RuntimeException {
//当某一个类继承RuntimeException,代表当前类为运行时异常,不需要标明方法可能抛出的异常
public MoneyNotEnoughException() {
super("抛出异常钱不够了");
}
}
public class CustomTest {
int a = 100;
public static void main(String[] args) {
Atm atm = new Atm();
atm.setMoney(100);
atm.add(100);
System.out.println("现有金钱:"+atm.money);
try {
atm.reduce(-300);
} catch (MoneyNotEnoughException | MoneyException m) {
System.out.println("现有金钱:"+atm.money);
m.printStackTrace();//打印异常信息
}
}
}
输出结果:
throw和throws的区别:
1、throws出现在方法函数头;而throw出现在函数体
2、throws表示可能出现异常;而执行throw语句则一定抛出了异常
今天学习分享的内容到此为止了,本是想要激励自己学下去,但发现快乐更是知识在于分享!
作为初学者对于Java的理解实在浅薄,如有错误请指正,万分感谢!