一、异常概述
1、什么是异常
异常就是程序运行过程中所出现的不正常现象。
try:把可能发生异常的代码包起来,当发生异常时,将异常抛出
catch:捕获异常并处理
finally:不管是否发生异常都会执行
throw:手动引发一个异常
throws:定义任何被调用方法的异常
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.println(1/0);
}catch(ArithmeticException e) {
System.out.println("运算错误");
e.printStackTrace();
}finally {
System.out.println("总会执行");
}
}
}
2、异常出现的原因
用户输入错误
代码的错误
环境的因素(内存满了?磁盘满了?等)
异常机制保证了程序的健壮性!
二、异常的分类
Throwable(Error、Exception)
Error:它是Java运行时的内部错误以及资源耗尽错误。很难恢复,不期望用户来处理。
Exception:RuntimeException(运行时异常)、非RuntimeException(由环境因素导致)
三、获取异常信息
try{
}catch(小异常){
}catch(大异常){
}
四、异常声明
异常声明指一个方法不处理它所产生的异常,而是调用层次向上传递,谁调用的这个方法谁来处理。
public class Test {
public static void main(String[] args) {
System.out.println("main,begin");
Test t=new Test();
try {
t.test1();
}catch(ArithmeticException e) {
System.out.println(e.getStackTrace());
}
System.out.println("main,end");
}
public void test1() throws ArithmeticException{
System.out.println("test开始执行");
System.out.println(1/0);
System.out.println("test执行完成");
}
}
五、手动抛出异常
throw exception;参数exception表示要抛出的异常对象,该对象是throwable类的子类,而且智能够是一个。
public class ThrowTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ThrowTest t=new ThrowTest();
try {
t.test();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void test() throws Exception {
throw new Exception("这是手动抛出来的");
}
}
try catch finally可以嵌套使用
public class TryCatchFinallyTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
double a=Math.random();
try {
if(a>0.5) {
System.out.println(a+"程序不报错");
}else {
throw new Exception();
}
}catch(Exception e) {
System.out.println("这是外层捕获的对象"+e);
try {
a=1/0;
}catch(ArithmeticException e1) {
System.out.println("这是内层捕获的对象"+e1);
}finally {
System.out.println("这是内层的finally块");
}
}finally {
System.out.println("这是外层的finally块");
}
}
}
六、异常链
两个或者多个不同的异常出现在同一个程序中,并且会发生嵌套抛出,我们称之为异常链。
public class ExceptionChainTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculator c=new Calculator();
try {
c.chufa(1,0);
} catch (NumberCalculateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("错误原因"+e);
}
}
}
class Calculator{
public int chufa(int a,int b) throws NumberCalculateException {
if(b==0) {
NumberCalculateException e=
new NumberCalculateException("计算错误");
NegativeNumberException e1=
new NegativeNumberException("除数不能为0");
e.initCause(e1);
throw e;
}
return 0;
}
}
class NegativeNumberException extends Exception{
public NegativeNumberException(String msg) {
super(msg);
}
}
class NumberCalculateException extends Exception{
public NumberCalculateException(String msg) {
super(msg);
}
}
七、定义自己的异常
1、继承Throwable
2、继承Exception
class 类名 extends Exception{
}
public class TestException {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyException me=new MyException("自己的异常类");
System.out.println(me.getMessage());
System.out.println(me.toString());
}
}
class MyException extends Exception{
public MyException() {
}
public MyException(String msg) {
super(msg);
}
}
使用自己的异常
public class AgeTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ageLevel(1000);
} catch (IllealAgeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static String ageLevel(int age) throws IllealAgeException {
if(age>=10&&age<=18) {
return "少年";
}else if(age>18&&age<=30) {
return "青年";
}else if(age>30&&age<=60) {
return "中年";
}else if(age>60&&age<=120) {
return "老年";
}else {
throw new IllealAgeException("非法的年龄");
}
}
}
class IllealAgeException extends Exception{
IllealAgeException(String msg){
super(msg);
}
}
八、小知识点
try不能单独出现,后面必须跟着catch或者finally或者两者都有。
来源:https://www.cnblogs.com/wangbobobobo/p/9788944.html