【异常的基本概念】_异常的其他概念笔记`
2014-10-01 22:28
183 查看
【异常的基本概念】_异常的其他概念笔记`
分类: Java【异常的基本概念】_异常的其他概念笔记`
本章目标:
掌握:throws与throw关键字的作用
掌握Exception与RuntimeException的区别
可以自定义异常类
了解断言的作用及应用
throws关键字
使用throws声明的方法表示此方法不处理异常,而是交给方法的调用处进行处理。
[java] view
plaincopyprint?
class Math{
public int div(int i,int j)throws Exception{
int temp = i/j;
return temp;
}
}
public class ThrowsDemo01{
public static void main(String[] args){
Math m = new Math(); //实例化Math类对象
System.out.println("除法操作:"+m.div(10,2));
}
}
编译时错误:
unreported exception java.lang.Exception;must be caught or declared to be thrown
[java] view
plaincopyprint?
class Math{
public int div(int i,int j)throws Exception{
int temp = i/j;
return temp;
}
}
public class ThrowsDemo01{
public static void main(String[] args){
Math m = new Math(); //实例化Math类对象
try{
System.out.println("除法操作:"+m.div(10,2));
}catch(Exception e){
e.printStackTrace();
}
}
}
如果现在连主方法的声明上也使用了throws关键字呢?那么是不是就意味着主方法也可以不处理异常了?
[java] view
plaincopyprint?
class Math{
public int div(int i,int j)throws Exception{
int temp = i/j;
return temp;
}
}
public class ThrowsDemo01{
public static void main(String[] args)throws Exception{
Math m = new Math(); //实例化Math类对象
try{
System.out.println("除法操作:"+m.div(10,2));
}catch(Exception e){
e.printStackTrace();
}
}
}
3.1.2 throw关键字
throw关键字的作用是在程序中抛出一个异常。抛出的时候抛出的是一个异常类的实例化对象。
在异常的处理中,try语句中要捕获的是一个异常的对象,那么此异常对象也可以自己抛出。
[java] view
plaincopyprint?
public class ThrowDemo02{
public static void main(String[] args){
try{
throw new Exception("自己抛着玩的。");
}catch(Exception e){
e.printStackTrace();
}
}
}
3.1.3范例:throw与throws的应用
在一般开发中try...catch...finally、throw、throws联合使用的情况是最多的,例如以下的代码:
[java] view
plaincopyprint?
class Math{
public int div(int i;int j)throws Exception{
System.out.println("****计算开始****");
int temp = 0; //定义局部变量
try{
temp = i/j; //计算,但是此处有可能出现异常
}catch(Exception e){
throw e;
}finally{
System.out.println("****计算结束****");
}
return temp;
}
}
public class ThrowDemo03{
public static void main(String[] args){
Math m = new Math();
try{
System.out.println("除法操作:"+m.div(10,0));
}catch(Exception e){
e.printStackTrace();
}
}
}
3.2 、Exception 与 RuntimeException的区别
[java] view
plaincopyprint?
public class RuntimeExceptionDemo01{
public static void main(String[] args){
String str = "123";
int temp = Integer.parseInt(str);
System.out.println(temp);
}
}
parseInt()方法的定义格式
public static int parseInt(String s)throws NumberFormatException
此处使用了throws关键字抛出了异常,为什么不用处理,编译也可能通过?
Exception 与 RuntimeException 的区别
这两个类的区别如下:
Exception 在程序中必须使用try...catch进行处理
RuntimeException 可以不使用try...catch进行处理,但是如果有异常产生,则异常将由JVM进行处理
Exception
RuntimeException
IllegalArgumentException
NumberFormatException
3.3自定义异常类
只需要继承Exception就可以完成自定义异常类。因为在JAVA提供的都是标准异常类,如果需要定义自己想要的异常信息的时候就可以自定义异常类。
只要直接继承Exception 类即可
[java] view
plaincopyprint?
class MyException extends Exception{//自定义异常类,继承Exception 类
public MyException(String msg){
super(msg); //调用Exception 类中有一个参数的构造方法,传递错误信息
}
}
public class DefaultException{
public static void main(String[] args){
try{
throw new MyException("自定义异常");
}catch(Excepton e){
e.printStackTrace();
}
}
}
一般如果项目很大的时候有可能自定义异常,可以得到一些准确的信息等等
3.4断言(assert)
在JDK1.4之后,系统增加了断言的功能。就是断定某一个操作的结果肯定是正确的,如果程序执行到出现断言语句的时候发现结果不正确了,则会出现错误的信息。
断言格式: assert boolean 表达式;
assert boolean 表达式:详细信息;
[java] view
plaincopyprint?
public class Test{
public static void main(String args[]){
int[] x = {1,2,3}; //定义数组,长度为3
assert x.length==0; //此处断言数组的长度为0
}
}
断言本身不会影响程序的执行,但是如果要想让一个断言起作用,则必须对断言进行验证。
-enableassertions ->可以简写为-ea
下面验证程序
java -ea Test.java
出现错误:Exception in Thread "main" java.lang.AssertionError
此断言信息为默认JAVA提供,如果觉得不满意,也可以自己设置错误的信息。
[java] view
plaincopyprint?
public class Test{
public static void main(String args[]){
int[] x = {1,2,3};
assert x.length==0:"数组长度不为0"; //此处断言数组的长度为0
}
}
此时,再进行断言验证
D:\expdemo>java -ea Test
Exception in thread "java.lang.AssertionError 数组长度不为0"
相关文章推荐
- 【异常的基本概念】_异常的其他概念笔记`
- 【异常的基本概念】_异常的基本概念笔记
- 【异常的基本概念】_异常的基本概念笔记
- 优秀课件笔记之经营决策常用的成本概念及基本方法
- 学习笔记-PPP and PPPoE基本概念
- Symbian学习笔记(2):基本概念
- [WCF 学习笔记] 1. 基本概念
- 孙鑫VC学习笔记:第十四讲 (一) 网络的基本概念
- oracle基本概念笔记
- Vega Prime自学笔记(1)基本概念
- [WCF 学习笔记] 1. 基本概念
- SOA学习笔记(一)----SOA基本概念
- 浙大面向对象程序设计笔记.类与对象的基本概念
- linux笔记09(linux的基本概念)
- 孙鑫VC学习笔记:第十四讲 网络的基本概念
- 优秀课件笔记之计算机的基本概念和分类
- Windows GDI学习笔记(1)——基本概念
- LoadRunner学习笔记 – LoadRunner的其他概念
- 孙鑫VC学习笔记:第十五讲 进程和线程基本概念
- [WCF 学习笔记] 1. 基本概念