我看别人的面经中有一道题目就问到了Exception,即java的异常处理的,我曾经也学了java的异常处理,可是我查了下,看了别人的博客关于写的exception异常处理。我发现,自己学的不坚固,仅仅学到了一点皮毛而已,在看了那么多博客和资料后。我做下总结吧,不然我大脑都混乱了。

java的中异常祖先是Throwable,Throwable的直接子类是Exception和Error。

Error通过单词我们就知道,是错误的意思。这样的错误通常是jvm执行产生的错误,出现这样的错误,我们的程序不能解决,比方内存溢出oom,堆溢出等。这样的错误。我们不必处理,直接让jvm抛出报错,我们没办法解决就无论了。

Exception中文意思是异常,那么Exception又分为检查性异常和非检查性异常。比方RuntimeException类及子类就是非检查性异处,表示执行时出现的异常,有数组越界,空指针异常。我们也能够不进行处理,让jvm自己抛出异常,当然假设我们能够预见这样的异常的话。最好在程序中进行推断检查,程序写健壮些。有的这样的异常就能够避免了。effect java有这样的处理的推荐,详细的能够看看这本书。

Exception另一类是检查性异常,这是除RuntimeException类及子类外的Exception类和Exception类的其它子类。

检查性异常,必需要进行异常处理的或者抛出,否则编译器会报错。

异常处理一般用到以下几个keywordtry,catch。finally。throws,throw,以下我逐个做一个说明。

try:

对于可能会抛出异常的代码一般放在try{}代码块中,比方:

try {
  int   i=2/0;
t.test1();
//System.out.println("kkk"+st);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("i will catch you....");
e.printStackTrace();
}

上面的代码2/0,肯定会抛出异常的。所以须要放在try{}块中,在try块中。假设哪一行抛出了异常,那么try块后面的代码将不再运行,将直接进入异常处理代码块。

catch:

catch(Excetion e){}这样的结构是用来处理。try代码块中抛出的异常的,能够有非常多的catch代码块,只是要注意的是,当有非常多catch代码块,而且catch里面的參数有父子关系的时候,catch里面的參数一定要是子类在前面,父类在后面。由于按catch代码块出现的先后顺序匹配抛出的异常,一旦抛出的异常是哪个catch里面參数的子类或同类,那么就能够被这个catch代码块进行处理。那么其它的catch代码块就算也符合这样的条件也不在进行处理了,假设有finally代码块的话就直接进入finally代码块中,没得则直接执行catch代码块后面的代码了。

finally:

finally代码块是异常处理中一定会运行的代码块,就算没得异常也会运行。有异常则进入catch代码块中处理后,在进入finally中处理。

在这里要注意一点,假如在try代码块中有return 语句。那么也是先运行finally里面的代码块后在运行return语句的。

还需注意一点的是。函数的返回值,是最后活动栈的栈顶的值。假设try进行return了,那么在finally中我也进行return。那么finally代码块里面的return的值在栈顶,所以会返回finally中return的值。有例如以下測试代码。

package com.wj.exception;

public class TestException3 {

	public String testException(){
try{
return "I am in try";
}finally{
return "I am in finally";
}
} public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3();
System.out.println(te.testException());
} }

执行结果是:

I am in finally

注意我这里是測试所以在finally中进行return了,一般不推荐在finally中进行return。

throws:

throws的使用方法是在      函数名(參数1,參数2.....)throws 异常类1,异常类2,异常类3.....{}

throws是表明这个函数可能会有异常产生,可是函数不进行异常处理,函数将向上抛异常,把异常处理交给函数的调用处进行处理。假设调用处。不进行处理则能够在进行向上抛。。

直到有地方进行try{}catch(){}异常处理,或者最后抛给jvm(不建议这么使用)。

package com.wj.exception;

public class TestException3 {

	public String testException() throws Exception{
/*try{
return "I am in try";
}finally{
return "I am in finally";
}*/
System.out.println("我不进行异常处理。我将异常向上抛。throws");
int i=2/0;
System.out.println("有异常我将得不到运行。。。。");
return "我是使用throws抛的异常";
} public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3();
try {
System.out.println(te.testException());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }

输出结果:

<span style="font-size:18px;">我不进行异常处理。我将异常向上抛,throws
java.lang.ArithmeticException: / by zero
at com.wj.exception.TestException3.testException(TestException3.java:13)
at com.wj.exception.TestException3.main(TestException3.java:24)
</span>

throw:

throw的作用是详细的抛出一个异常类对象。一般在方法里面使用。当在try代码块中使用的时候。直接进入catch代码块中进行处理。

比方:

package com.wj.exception;

public class TestException3 {

	public String testException(){
try{
throw new Exception("i is throw exception");
}catch(Exception e){
System.out.println("我处理throw抛出的异常");
}finally{
System.out.println("我运行finally代码块\n"); }
System.out.println("异常处理结束! ! !");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }

同意结果:

我处理throw抛出的异常

我运行finally代码块





异常处理结束!!



I am in finally

throw手动抛出的异常须要从里到到外查找处理的try代码块。没有处理话,就须要把方法后面加throws,把异常向上抛。看例如以下代码:

1

package com.wj.exception;

public class TestException3 {

	public String testException(){
try{
int i=3/0;
}catch(Exception e){
throw new Exception();
}finally{
System.out.println("我运行finally代码块\n"); }
//System.out.println("异常处理结束。。! ");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }

2

上面的代码在编译器里面会出现错误,提示我们使用throws抛出异常,或者把throw new Exception()进行因此处理。改成这种

<pre name="code" class="java">package com.wj.exception;

public class TestException3 {

	public String testException() {
try{
int i=3/0;
}catch(Exception e){
try {
throw new Exception();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}finally{
System.out.println("我运行finally代码块\n"); }
//System.out.println("异常处理结束!。!");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }


输出结果:

java.lang.Exception
at com.wj.exception.TestException3.testException(TestException3.java:11)
at com.wj.exception.TestException3.main(TestException3.java:32)
我运行finally代码块 I am in finally

3

或者说是这种:

<pre name="code" class="java">package com.wj.exception;

public class TestException3 {

	public String testException()  throws Exception{
try{
int i=3/0;
}catch(Exception e){ throw new Exception(); }finally{
System.out.println("我运行finally代码块\n");
}
//System.out.println("异常处理结束! !。");
return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); try {
System.out.println(te.testException());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

输出结果:


我运行finally代码块

java.lang.Exception
<span style="white-space:pre"> </span>at com.wj.exception.TestException3.testException(TestException3.java:11)
<span style="white-space:pre"> </span>at com.wj.exception.TestException3.main(TestException3.java:28)

4

这样改,非常easy,可是另一种更加奇葩的改法。能够使编译器不出错,例如以下:

package com.wj.exception;

public class TestException3 {

	public String testException()  {
try{
int i=3/0;
}catch(Exception e){ throw new Exception(); }finally{
System.out.println("我运行finally代码块\n");
return "I am in finally";
}
//System.out.println("异常处理结束! ! ! ");
//return "I am in finally"; } public static void main(String[] args) {
// TODO Auto-generated method stub TestException3 te=new TestException3(); System.out.println(te.testException()); } }

输出:

我运行finally代码块

I am in finally

发现没有第四个代码。本来是跑出了一个异常的,可是没有输出不论什么的异常信息。

并且把函数返回值改为void,在finally里面不使用return。则又会报错,这一点我不是非常明确了。难道在catch里面使用throw抛出的异常,在finally代码块里面使用return语句后,就被处理了嘛?要是被处理了怎么没得异常输出,还是输出的异常信息在栈里面被覆盖了?知道怎么回事的朋友,能够交流下。

看了上面那么多,你是否对java中的异常有一定理解了?

我在博客http://blog.csdn.net/hguisu/article/details/6155636里面看到一个样例,貌似比較经典。就借用下了,代码例如以下了:

package com.wj.exception;

public class TestException1 {

	boolean testEx() throws Exception {
boolean ret = true;
try {
ret = testEx1();
} catch (Exception e) {
System.out.println("testEx, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx, finally; return value=" + ret);
return ret;
}
} boolean testEx1() throws Exception {
boolean ret = true;
try {
ret = testEx2();
if (!ret) {
return false;
}
System.out.println("testEx1, at the end of try");
return ret;
} catch (Exception e) {
System.out.println("testEx1, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx1, finally; return value=" + ret);
return ret;
}
} boolean testEx2() throws Exception {
boolean ret = true;
try {
int b = 12;
int c;
for (int i = 2; i >= -2; i--) {
c = b / i;
System.out.println("i=" + i);
}
return true;
} catch (Exception e) {
System.out.println("testEx2, catch exception");
ret = false;
throw e;
} finally {
System.out.println("testEx2, finally; return value=" + ret);
return ret;
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub TestException1 testException1 = new TestException1();
try {
testException1.testEx();
} catch (Exception e) {
System.out.println("结束\n");
e.printStackTrace();
} } }

假设不看答案,你能说出执行结果嘛?你能够到自己的编译器中执行下看下结果,在进行debug下,看看是否和你想的一样了。

上面执行的结果例如以下:

i=2

i=1

testEx2, catch exception

testEx2, finally; return value=false

testEx1, finally; return value=false

testEx, finally; return value=false

我进行过debug。所以我清楚执行的流程,可是对于内部的机制,还有点疑点,就像我在4所说的。finally中的return 会覆盖throw抛出的异常一样。真的是这种嘛,假设是这种话。上面的代码就能分析出和执行一样的输出结果了,能够试试,一起交流下。

本文原创,转载请著明:http://blog.csdn.net/j903829182/article/details/39808307

java异常处理Exception的更多相关文章

  1. Java异常处理:如何写出“正确”但被编译器认为有语法错误的程序

    文章的标题看似自相矛盾,然而我在"正确"二字上打了引号.我们来看一个例子,关于Java异常处理(Exception Handling)的一些知识点. 看下面这段程序.方法pleas ...

  2. java 异常处理 Throwable Error 和Exception

    Java异常类层次结构图:       异常的英文单词是exception,字面翻译就是“意外.例外”的意思,也就是非正常情况.事实上,异常本质上是程序上的错误,包括程序逻辑错误和系统错误. 比如使用 ...

  3. Java自学-异常处理 Exception

    Java 异常 Exception 异常定义: 导致程序的正常流程被中断的事件,叫做异常 步骤 1 : 文件不存在异常 比如要打开d盘的LOL.exe文件,这个文件是有可能不存在的 Java中通过 n ...

  4. Java异常处理总结Exception\Error

    Java异常处理总结Exception\Error 2012-12-28 08:17:17|  分类: JAVA |  标签:java  |举报|字号 订阅   Java异常处理总结          ...

  5. 札记:Java异常处理

    异常概述 程序在运行中总会面临一些"意外"情况,良好的代码需要对它们进行预防和处理.大致来说,这些意外情况分三类: 交互输入 用户以非预期的方式使用程序,比如非法输入,不正当的操作 ...

  6. java异常处理(父子异常的处理)

    我当初学java异常处理的时候,对于父子异常的处理,我记得几句话“子类方法只能抛出父类方法所抛出的异常或者是其子异常,子类构造器必须要抛出父类构造器的异常或者其父异常”.那个时候还不知道子类方法为什么 ...

  7. Java 异常处理

    异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用System.out ...

  8. 《转载》Java异常处理的10个最佳实践

    本文转载自 ImportNew - 挖坑的张师傅 异常处理在编写健壮的 Java 应用中扮演着非常重要的角色.异常处理并不是功能性需求,它需要优雅地处理任何错误情况,比如资源不可用.非法的输入.nul ...

  9. Java异常处理机制 try-catch-finally 剖析

    Java拥有着强大的异常处理机制,最近初步学习了下,感觉内容还是挺多的,特此来将自己的理解写出来与大家分享. 一. 在Java代码code中,由于使用Myeclipse IDE,可以自动提醒用户哪里有 ...

随机推荐

  1. AC自动机及KMP练习

    好久都没敲过KMP和AC自动机了.以前只会敲个kuangbin牌板子套题.现在重新写了自己的板子加深了印象.并且刷了一些题来增加自己的理解. KMP网上教程很多,但我的建议还是先看AC自动机(Trie ...

  2. 【判断解是否可行-二分】POJ1064-Cable master

    http://poj.org/problem?id=1064 [题目大意] 给出几条绳子的长度,问如果要切出k条长度相等的绳子,这k条绳子最常多长? [思路] 二分.把下界设为0,上界设为所有绳子长度 ...

  3. 【枚举+贪心】POJ2718-Smallest Difference

    [题目大意] 按升序输出几个不同的数字,任意组成两个数字,输出最小的差值. [思路] 虽然是在穷竭搜索的章节里找到的题目,但是我觉得不需要穷竭搜索,枚举一下就可以了,0MS.分为一下三种情况: (1) ...

  4. 1.1(SQL学习笔记)SQL基础

    一.SELECT 建表及数据填充语句下载:链接: https://pan.baidu.com/s/1WHYafwqKJEKq1kDwCH_Zlg 提取码: 3wy4 SELECT用于指定检索返回的结果 ...

  5. ajax 同步和异步的区别

    举个例子:普通B/S模式(同步)AJAX技术(异步)同步:提交请求->等待服务器处理->处理完毕返回 这个期间客户端浏览器不能干任何事异步: 请求通过事件触发->服务器处理(这时浏览 ...

  6. [转]iBatis简单入门教程

    iBatis 简介: iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快.如果不需要太多复杂的功能,iBatis 是能够满足 ...

  7. LOG收集系统(一):原日志至收集

    Date: 20140207Auth: Jin 设置一个LOG收集系统1. 收集原生(不解析,不压缩)的业务日志和WEB日志(NGINX,PHP)2. 提供给开发,测试直接阅读和下载 需求分析原生日志 ...

  8. 基于tiny4412的Linux内核移植 -- eMMC驱动移植(六)

    作者信息 作者: 彭东林 邮箱:pengdonglin137@163.com QQ:405728433 平台简介 开发板:tiny4412ADK + S700 + 4GB Flash 要移植的内核版本 ...

  9. 【java】LIst切割----划分 List为几个LIst的几种工具类 1.按照目标份数划分 2.按照目标容量划分 【适用场景:mybatis分批次In查询,放置In拼接sql过长】

    如题,示例代码如下: /** * 1> 按照份数---划分list * @param source * @param num 想要划分成多少份 * @return */ public stati ...

  10. 支持向量机(SVM)介绍

    一.了解支持向量机   支持向量机(support vector machings, SVM) 是一种二分类模型,其基本模型定义为特征空间上的间隔最大的线性分类器,其学习策略便是间隔最大化,最终可转化 ...