1.Java中所有的异常类都会直接或间接地继承自Exception。

2.RuntimeException类也是直接继承自Exception类,它叫做运行时异常,Java中所有的运行时异常都会直接或间接地继承自RuntimeException。

3.Java中凡是继承自Exception而不是继承自RuntimeException的类都是非运行时异常。

4.异常处理的一般结构是:

try

{

}

catch(Exception e)

{

}

finally

{

}

无论程序是否出现异常,finally块中的代码都是会被执行的。

 public class ExceptionTest
{
public static void main(String[] args) {
int c = ;
try {
int a = ;
int b = ;
// 出现异常后try中后面的代码不再运行
c = a / b;// 每一个异常都是一个类,生成ArithmeticException的对象。
System.out.println("hello world");
}
// 可以有多个catch
catch (ArithmeticException e) {
e.printStackTrace();// 打印栈的轨迹
}
// finally的代码一定会运行
finally {
System.out.println("welcome");
}
System.out.println(c);
}
}

5.对于非运行时异常(checked exception),必须要对其进行处理,处理方式有两种:第一种是使用try.. catch…finally进行捕获;第二种是在调用该会产生异常的方法所在的方法声明throws Exception

throws Exception

 public class ExceptionTest2
{
//使用throws Exception抛出异常,当遇到异常时,程序暂停
public void method() throws Exception//应为在方法中会抛出异常
{
System.out.println("hello world");
throw new Exception();
}
public static void main(String[] args)
{
ExceptionTest2 test = new ExceptionTest2();
try
{
test.method();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
System.out.println("aaa");
}
}
}

6.对于运行时异常(runtime exception),我们可以不对其进行处理,也可以对其进行处理。推荐不对其进行处理。

7.NullPointerException是空指针异常,出现该异常的原因在于某个引用为null,但你却调用了它的某个方法。这时就会出现该异常。

8.所谓自定义异常,通常就是定义了一个继承自Exception类的子类,那么这个类就是一个自定义异常类。通常情况下,我们都会直接继承自Exception类,一般不会继承某个运行时的异常类。

9.我们可以使用多个catch块来捕获异常,这时需要将父类型的catch块放到子类型的catch块之后,这样才能保证后续的catch可能被执行,否则子类型的catch将永远无法到达,Java编译器会报编译错误;如果多个catch块的异常类型是独立的(MyException, MyException2), 那么谁前谁后都是可以的。

 public class MyException extends Exception
{
public MyException()
{
super();
} public MyException(String message)
{
super(message);
}
}
public class MyException2 extends Exception
{
public MyException2()
{
super();
} public MyException2(String message)
{
super(message);
}
} public class ExceptionTest4
{
public void method(String str) throws Exception
{
if(null == str)
{
throw new MyException("传入的字符串参数不能为null");
}
else if("hello".equals(str))//把常亮放在前面,防止空指针异常
{
throw new MyException2("传入的字符串不能为hello");
}
else
{
System.out.println(str);
}
}
public static void main(String[] args)
{
try
{
ExceptionTest4 test = new ExceptionTest4(); test.method("hello");
}
//catch块按顺序匹配
catch(MyException e)
{
System.out.println("进入到MyException catch块");
e.printStackTrace();
}
catch(MyException2 e)
{
System.out.println("进入到MyException2 catch块");
e.printStackTrace();
}
//这个catch不会运行
catch(Exception e)
{
System.out.println("进入到Exception catch块");
e.printStackTrace();
}
finally
{
System.out.println("异常处理完毕");
}
System.out.println("程序执行完毕");
}
}

10.如果try块中存在return语句,那么首先也需要将finally块中的代码执行完毕,然后方法再返回。

11.如果try块中存在System.exit(0)语句,那么就不会执行finally块中的代码,因为System.exit(0)会终止当前运行的Java虚拟机,程序会在虚拟机终止前结束执行。

异常(Exception)的更多相关文章

  1. Atitit java的异常exception 结构Throwable类

    Atitit java的异常exception 结构Throwable类 1.1. Throwable类 2.StackTrace栈轨迹1 1.2. 3.cause因由1 1.3. 4.Suppres ...

  2. 05_Java异常(Exception)

    1. 异常的概念 1.1什么是异常 异常指的是程序运行时出现的不正常情况. 1.2异常的层次 Java的异常类是处理运行时的特殊类,每一种异常对应一种特定的运行错误.所有Java异常类都是系统类库中E ...

  3. 异常Exception in thread "AWT-EventQueue-XX" java.lang.StackOverflowError

    今天太背了,bug不断,检查到最后都会发现自己脑残了,粗心写错,更悲剧的是写错的时候还不提示错. 刚才有遇到一个问题,抛了这个异常Exception in thread "AWT-Event ...

  4. Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject

    18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...

  5. 异常 Exception 堆栈跟踪 异常捕获 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. 理解Python语言里的异常(Exception)

    Exception is as a sort of structured "super go to".异常是一种结构化的"超级goto". 作为一个数十年如一日 ...

  7. PL/SQL 08 异常 exception

    --PL/SQL错误  编译时  运行时 --运行时的出错处理  EXCEPTION --异常处理块DECLARE …BEGIN …EXCEPTION WHEN OTHERS THEN  handle ...

  8. 【马克-to-win】学习笔记—— 第五章 异常Exception

    第五章 异常Exception [学习笔记] [参考:JDK中文(类 Exception)] java.lang.Object java.lang.Throwable java.lang.Except ...

  9. 【异常】Maxwell异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: <EOF> after : ""

    1 详细异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at ...

  10. 异常-Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: <EOF> after :

    1 详细异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at ...

随机推荐

  1. linux fork函数与vfork函数

    一.fork1. 调用方法#include <sys/types.h>#include <unistd.h> pid_t fork(void);正确返回:在父进程中返回子进程的 ...

  2. Oracle sql查询

    http://blog.csdn.net/jlds123/article/details/6572559

  3. android dialog 原来dialog对话框也有自己的按键监听事件 onKeyDown方法

    探讨在一个activity中按menu键时弹出自己定义的dialog(自定义菜单对话框)时,再按一次手机的menu键发现这个自定义的dialog菜单并没有关闭,原来是这个dialog内部也有onKey ...

  4. 省市区 Mysql 数据库表

    1.查省SELECT * FROM china WHERE china.Pid=02.查市SELECT * FROM chinaWHERE china.Pid=3300003.查区SELECT * F ...

  5. centos更新163源并升级内核

    使用说明 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...

  6. Java的类演进过程

    1.从面向过程到面向对象 在大家最熟悉的C语言中,如果要定义一个复杂的数据类型就用结构体(Struct)来实现,而为结构体的每个操作都定义一个函数,这个函数与结构体本身的定义没有任何关系.程序的重心集 ...

  7. node.js模块之util模块

    util提供了各种使用的工具.require('util') to access them. Util.format(format,[..]) Returns a formatted string u ...

  8. js团购倒计时

    客户端代码可以看: http://www.zhangxinxu.com/wordpress/2010/07/%E5%9B%A2%E8%B4%AD%E7%B1%BB%E7%BD%91%E7%AB%99% ...

  9. PHP实现浏览历史记录

    http://www.3a88.com/service/206.html http://www.1360.cc/ZhanChangJiaoCheng/6831.html http://www.osch ...

  10. stdio.h及cstdio的区别

    2013-07-04 16:45:19 找了很多资料,没有说的很明白的,下面是老外的一篇文章,解释的比较清楚,后面给出翻译. Clarifying stdio.h versus cstdio 转自:h ...