Java 异常处理
来源:互联网 发布:淘宝官方网站登录网站 编辑:程序博客网 时间:2024/06/06 10:02
一、什么是异常处理?
在程序运行过程中,可能会有一些逻辑错误,比如一个数除以0,这时候就发生了异常。你可以不处理异常,那么程序就会崩溃,后面的内容都无法运行。
例如:
package test;public class HelloWorld { public static void main(String[] args){ int result = divide(4,0); System.out.println(result); } public static int divide(int x, int y){ int result = x / y; return result; }}
程序运行结果为:
Exception in thread “main” java.lang.ArithmeticException: / by zero
at test.HelloWorld.divide(HelloWorld.java:10) at
test.HelloWorld.main(HelloWorld.java:5)
这个时候程序因为除以0发生了异常,后面的代码无法继续运行。
如果我们想程序能够处理异常并继续运行,可以通过try…catch语法来对异常进行处理:
package test;public class HelloWorld { public static void main(String[] args){ try{ int result = divide(4,0); System.out.println(result); }catch (Exception e){ System.out.println("捕获到异常:" + e.getMessage()); System.out.println("处理该异常...."); } System.out.println("程序继续执行"); } public static int divide(int x, int y) { int result = x / y; return result; }}
运行结果为:
捕获到异常:/ by zero
处理该异常….
程序继续执行
这个时候,我们可以捕获到这个异常,并进行一定的修复处理。try后面的代码不会继续运行,但是其它后面的语句可以继续运行。这样就不会因为有异常产生而导致程序强制结束。
二、finally关键字
此外,如果异常发生或者异常没发生都有一些代码要执行的话(如释放资源),那么可以通过finally关键字完成:
package test;public class HelloWorld { public static void main(String[] args){ try{ int result = divide(4,0); System.out.println(result); }catch (Exception e){ System.out.println("捕获到异常:" + e.getMessage()); System.out.println("处理该异常...."); }finally{ System.out.println("无论是否异常,释放资源..."); } System.out.println("程序继续执行"); } public static int divide(int x, int y) { int result = x / y; return result; }}
三、throws关键字
如果divide是别人写的函数,我们通常情况不会去看divide函数的实现,但是divide函数确实会发生异常。针对这种情况,我们可以使用throws关键字来为函数声明它会抛出异常:
package test;public class HelloWorld { public static void main(String[] args){ try{ int result = divide(4,0); System.out.println(result); }catch (Exception e){ System.out.println("捕获到异常:" + e.getMessage()); System.out.println("处理该异常...."); }finally{ System.out.println("无论是否异常,释放资源..."); } System.out.println("程序继续执行"); } public static int divide(int x, int y) throws Exception { int result = x / y; return result; }}
如果有throws关键字,就说明这个函数要求使用者提供异常处理。如果使用者没有实现异常处理,那么编译的时候就会给出错误:
Exception in thread “main” java.lang.Error: Unresolved compilation
problem: Unhandled exception type Exception
四、自定义异常
为了描述程序中特有的异常情况,你也可以继承Exception类来实现自己的异常类:
package test;public class HelloWorld { public static void main(String[] args){ try{ int result = divide(4,-1); System.out.println(result); }catch (Exception e){ System.out.println("捕获到异常:" + e.getMessage()); System.out.println("处理该异常...."); }finally{ System.out.println("无论是否异常,释放资源..."); } System.out.println("程序继续执行"); } public static int divide(int x, int y) { if(y<0){ throw new DivideByMinusException("被除数是负数"); } int result = x / y; return result; }}class DivideByMinusException extends Exception{ public DivideByMinusException(){ super(); } public DivideByMinusException(String message){ super(message); }}
- 【java】java异常处理
- java异常和异常处理
- Java 异常及异常处理
- java异常与异常处理
- Java异常处理-----自定义异常
- Java异常与异常处理
- java异常及异常处理
- Java 异常与异常处理
- Java异常与异常处理
- Java异常及异常处理
- Java异常处理--自定义异常
- JAVA异常之异常处理
- Java异常处理-----自行处理
- Java异常处理-----抛出处理
- Java异常处理-----抛出处理
- java异常处理
- java 异常处理分析
- java异常处理
- Android 递归栈溢出问题解决
- 郑轻OJ zzulioj 1068: 二进制数
- android studio改变主题字体大小
- 奖券数目
- 前端优化建议
- Java 异常处理
- 项目经理面试中常被问到的12问题
- 【红宝书笔记】原型模式-prototype
- HashMap实现原理分析
- 【实例】 python数据库编程
- Android开发应用程序发布到Google Play步骤
- Git 常用命令速查表
- JSP编译成Servlet(一)语法树的生成——语法解析
- string类 sort Educational Codeforces Round 9 C. The Smallest String Concatenation