java 异常处理工具类_3.1 异常与异常处理(常用工具类)

1、异常简介

异常简介

异常:阻止当前方法或作用域的问题

异常处理的作用

Java异常体系结构简介

Throwable:

Error:虚拟机错误(VirtualMachineError)、线程锁死(ThreadDeath)--奔溃

Exception:编码、环境、用户操作输入出现问题

1、RuntimeException((非检查异常):

空指针异常(NullPointerException)

String str=null;

System.out.println(str.length());

数组下标越界异常(ArrayIndexOutOfBoundsException)

int[] ary={1,2,3};

for(int i=0;i<=3;i++){

System.out.println(ary[i]);

}

类型转换异常(ClassCastException)

class Animal{

}

class Dog extends Animal{

}

class Cat extends Animal{

}

public class Test{

public static void main(String[] args){

Animal a1=new Dog();

Animal a2=new Cat();

Dog d1=(Dog)a1;

Dog d2=(Dog)a2;

}

}

算术异常(ArithmeticException)

int one=12;

int two=0;

System.out.println(one/two);

2、检查异常

文件异常(IOException)

SQL异常(SQLException)

处理异常

try-catch以及try-catch-finally

抛出异常

自定义异常

异常

2、使用try、catch、finally

try-catch以及try-catch-finally

try{

//一些会抛出异常的方法

}catch(Exception e){

//处理该异常的代码块

}

try{

System.out.println("请输入你的年龄:");

Scanner input=new Scanner(System.in);

int age=input.nextInt();

System.out.println("十年之后你"+(age+10)+"岁";

}catch(InputMismatchException e){

System.out.println("您应该输入整数!!!");

}

System.out.println("程序结束!");

package study;

public class TryCatchTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

TryCatchTest tct=new TryCatchTest();

int result=tct.test();

System.out.println("test()方法执行完毕!返回值为:"+result);

int result2=tct.test2();

System.out.println("我想大声告诉你!test2()执行完毕");

}

/**

* divider(除数);

* result(结果);

* try-catch捕获while循环

* 每次循环,divider减一,result=result+100/divider

* 如果:捕获异常,打印输出“抛出异常了!!!”,返回-1

* 否则:返回result

*/

public int test(){

int divider=10;

int result=100;

try{

while(divider>-1){

divider--;

result=result+100/divider;

}

return result;

}catch(Exception e){

e.printStackTrace();

System.out.println("循环抛出异常了!!!");

return-1;

}

}

/**

* divider(除数);

* result(结果);

* try-catch捕获while循环

* 每次循环,divider减一,result=result+100/divider

* 如果:捕获异常,打印输出“抛出异常了!!!”,返回result=999

* 否则:返回result

* finally:打印输出“这是finally!!!哈哈!!”,同时打印输出result的值

*/

public int test2(){

int divider=10;

int result=100;

try{

while(divider>-1){

divider--;

result=result+100/divider;

}

return result;

}catch(Exception e){

e.printStackTrace();

System.out.println("循环抛出异常了!!!");

return result=999;

}finally{

System.out.println("这是finally!!!哈哈!!");

System.out.println("我是result,我的值是:"+result);

}

}

}

循环抛出异常了!!!

test()方法执行完毕!返回值为:-1

java.lang.ArithmeticException: / by zero

at study.TryCatchTest.test(TryCatchTest.java:25)

at study.TryCatchTest.main(TryCatchTest.java:8)

循环抛出异常了!!!

这是finally!!!哈哈!!

我是result,我的值是:999

我想大声告诉你!test2()执行完毕

java.lang.ArithmeticException: / by zero

at study.TryCatchTest.test2(TryCatchTest.java:52)

at study.TryCatchTest.main(TryCatchTest.java:10)

package study;

public class TryCatchTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

TryCatchTest tct=new TryCatchTest();

int result3=tct.test3();

System.out.println("我想大声告诉你!test3()执行完毕");

}

/**

* divider(除数);

* result(结果);

* try-catch捕获while循环

* 每次循环,divider减一,result=result+100/divider

* 如果:捕获异常,打印输出“抛出异常了!!!”

* 否则:返回result

* finally:打印输出“这是finally!!!哈哈!!”,同时打印输出result的值

*最后返回1111作为结果

*/

public int test3(){

int divider=10;

int result=100;

try{

while(divider>-1){

divider--;

result=result+100/divider;

}

}catch(Exception e){

e.printStackTrace();

System.out.println("循环抛出异常了!!!");

}finally{

System.out.println("这是finally!!!哈哈!!");

System.out.println("我是result,我的值是:"+result);

}

System.out.println("我是test3(),我运行完了");

return 1111;

}

}

java.lang.ArithmeticException: / by zero

at study.TryCatchTest.test3(TryCatchTest.java:25)

at study.TryCatchTest.main(TryCatchTest.java:6)

循环抛出异常了!!!

这是finally!!!哈哈!!

我是result,我的值是:381

我是test3(),我运行完了

我想大声告诉你!test3()执行完毕

综上:如果try-catch-finally语句块之内没有return语句,则会调用这三个语句块之外的return语句

3、java中的异常抛出以及自定义异常

java中的抛出异常

throw:将产生的异常抛出(动作)

写在具体的方法体中,表示抛出异常的这个动作,如果某个方法调用到了会抛出异常的方法,那么必须添加try-catch语句来尝试捕获这种异常,或者添加throw声明来将异常抛出给更上一层的调用者去进行处理

public void divide(int one,int two)throws Exception{

if(two==0)

throw new Exception("两数相除,除数不能为0!");

else

System.out.println("两数相除,结果为:"+one/two);

}

(一)

public void compute(){

/**

代码省略。。。

*/

try{

divide(5,0);

}catch(Exception e){

System.out.println(e.getMessage());

}

}

(二)

public void compute() throws Exception{

/**

代码省略。。。

*/

divide(5,0);

}

throws:声明将要抛出何种类型的异常(声明)

public void 方法名(参数列表)

throws 异常列表{

//调用会抛出异常的方法或者:

throw new Exception();

}

自定义异常

class 自定义异常类 extends 异常类型{

}

自定义异常必须继承于Java标准类库中意思相近的异常类型或者直接继承于所有异常类型的基类(Exception)

public class DrunkException extends Exception{

public DrunkException(){

}

public DrunkException(String message){

super(message);

}

}

4、Java中的异常链

有时候可以把捕获的异常包装成一个新的异常,然后再新的异常中添加对原始异常的引用,再把新异常抛出--异常链

package study;

public class ChainTest {

/**

* test1():抛出"喝大了"异常

* test2():调用test(),捕获"喝大了"异常,并且包装成运行时异常,继续抛出

* main方法中,调用test2,尝试捕获test2()方法抛出的异常

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

ChainTest ct=new ChainTest();

try{

ct.test2();

}catch(Exception e){

e.printStackTrace();

}

}

public void test1()throws DrunkException{

throw new DrunkException("喝酒别开车");

}

public void test2()throws DrunkException{

try{

test1();

}catch(DrunkException e){

RuntimeException newExc=new RuntimeException("司机一滴酒,亲人两行泪");

newExc.initCause(e);

throw newExc;

}

}

}

java.lang.RuntimeException: 司机一滴酒,亲人两行泪

at study.ChainTest.test2(ChainTest.java:26)

at study.ChainTest.main(ChainTest.java:14)

Caused by: study.DrunkException: 喝酒别开车

at study.ChainTest.test1(ChainTest.java:20)

at study.ChainTest.test2(ChainTest.java:24)

... 1 more

5、总结

1、对运行时的异常可以采用优化代码,用逻辑加以控制去合理的规避同时辅助try-catch处理

2、在多重catch块后面,可以加一个catch(Exception)来处理被遗漏的异常

3、对于不确定的代码,也可以加上try-catch,处理潜在的异常

4、尽量去处理异常,切忌只是简单的调用printStaticTrace()去打印输出

5、具体如何处理异常,要根据不同的业务需求和异常类型去决定

6、尽量添加finally语句块去释放占用的资源

c497099298ca

Paste_Image.png

c497099298ca

Paste_Image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值