异常(Exception)
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)的更多相关文章
- Atitit java的异常exception 结构Throwable类
Atitit java的异常exception 结构Throwable类 1.1. Throwable类 2.StackTrace栈轨迹1 1.2. 3.cause因由1 1.3. 4.Suppres ...
- 05_Java异常(Exception)
1. 异常的概念 1.1什么是异常 异常指的是程序运行时出现的不正常情况. 1.2异常的层次 Java的异常类是处理运行时的特殊类,每一种异常对应一种特定的运行错误.所有Java异常类都是系统类库中E ...
- 异常Exception in thread "AWT-EventQueue-XX" java.lang.StackOverflowError
今天太背了,bug不断,检查到最后都会发现自己脑残了,粗心写错,更悲剧的是写错的时候还不提示错. 刚才有遇到一个问题,抛了这个异常Exception in thread "AWT-Event ...
- 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 ...
- 异常 Exception 堆栈跟踪 异常捕获 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 理解Python语言里的异常(Exception)
Exception is as a sort of structured "super go to".异常是一种结构化的"超级goto". 作为一个数十年如一日 ...
- PL/SQL 08 异常 exception
--PL/SQL错误 编译时 运行时 --运行时的出错处理 EXCEPTION --异常处理块DECLARE …BEGIN …EXCEPTION WHEN OTHERS THEN handle ...
- 【马克-to-win】学习笔记—— 第五章 异常Exception
第五章 异常Exception [学习笔记] [参考:JDK中文(类 Exception)] java.lang.Object java.lang.Throwable java.lang.Except ...
- 【异常】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 ...
- 异常-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 ...
随机推荐
- spoj 247
不管行列 总是先切割切割费用大的 代码比较烂 ...... #include <iostream> #include <cstdio> #include <cstr ...
- 读取tiled地图
原地址:http://www.unity蛮牛.com/thread-20854-1-1.html Tile是一个非常好用的地图编辑器,一直以来我都在找支持tilemap的unity2D插件,但是找 ...
- swift苹果的下一代语言
http://numbbbbb.github.io/the-swift-programming-language-in-chinese/chapter1/01_swift.html 有时间再看,bas ...
- ecos内核概览--bakayi译
http://blog.csdn.net/wangzaiwei2006/article/details/6453423
- 转:tar 常用命令
tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数: -x : 解压缩压缩档案的参数: -z : 是 ...
- matlab 在代码中,显示错误,退出程序
使用函数error('message_id', 'message'),出现错误时函数中止运行. 参考http://www.ilovematlab.cn/thread-43261-1-1.html
- WCF服务运行一段时间后客户端无法连接WCF服务的解决办法 (转)
WCF服务运行一段时间后客户端无法连接WCF服务的解决办法 (转) Windows Communication Foundation (WCF)是Microsoft为构建面向服务的应用提供的分布式通信 ...
- 条件与(&&)和逻辑与(&)以及条件或(||)和逻辑或(|)区别
条件与(&&)和逻辑与(&)以及条件或(||)和逻辑或(|)区别在于它们的运算结果是不相同的. 条件与(&&)和条件或(||)采用的是所谓的"短路规则 ...
- Fast scroller styles
<!-- Fast scroller styles --> <!-- Drawable to use as the fast scroll thumb. --> <att ...
- 【HDOJ】4358 Boring counting
基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...