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. DJANGO技巧两则:模拟MKDIR -P及配合NGINX上传大文件不使超时

    这都是在开发当哪遇到的问题,网上转转,作个记录: http://blog.chinaunix.net/uid-25525723-id-1596574.html http://bookshadow.co ...

  2. hdu 3853 LOOPS 概率DP

    简单的概率DP入门题 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include ...

  3. http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

    http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

  4. Qt:QT右键菜单

    Qt QTableView 上加右键弹出菜单, 并复制选中的单元格内容到剪贴板中 http://wenku.baidu.com/view/c51cfb63cf84b9d528ea7a29.html h ...

  5. 217. Contains Duplicate

    题目: Given an array of integers, find if the array contains any duplicates. Your function should retu ...

  6. 判断浏览器类型-----------navigator.userAgent.indexOf()

    <script language="JavaScript"> <!-- function getOs() { var OsObject = "" ...

  7. sdut 1570 c旅行

    用搜索(bfs,dfs)做了半天,都超时,原来是dp; 参考博客:http://www.cnblogs.com/liuzezhuang/archive/2012/07/29/2613820.html ...

  8. mysql 读取硬盘数据

    innodb 的最小管理单位是页 innodb的最小申请单位是区,一个区 1M,内含64个页,每个页16K ,即 64*16K=1M, 考虑到硬盘局部性,每次读取4个区,即读4M的数据加载至内存 线性 ...

  9. Flash挡住DIV的解决方法

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  10. javascript两行代码按指定格式输出日期时间

    javascript两行代码按指定格式输出日期时间,具体看代码: function date2str(x,y) { var z ={y:x.getFullYear(),M:x.getMonth()+1 ...