java异常处理之throw, throws,try和catch
2014-10-19 16:39
525 查看
程序运行过程中可能会出现异常情况,比如被0除、对负数计算平方根等,还有可能会出现致命的错误,比如内存不足,磁盘损坏无法读取文件等,对于异常和错误情况的处理,统称为异常处理。
Java异常处理主要通过5个关键字控制:try、catch、throw、throws和finally。try的意思是试试它所包含的代码段中是否会发生异常;而catch当有异常时抓住它,并进行相应的处理,使程序不受异常的影响而继续执行下去;throw是在程序中明确引发异常;throws的作用是如果一个方法可以引发异常,而它本身并不对该异常处理,那么它必须将这个异常抛给调用它的方法;finally是无论发不发生异常都要被执行的代码。
关键字:throw,throws,try和catch的用法如下:
1、throws出现在方法的声明中,表示该方法可能会抛出的异常,允许throws后面跟着多个异常类型。
2、throw出现在方法体中,用于抛出异常。当方法在执行过程中遇到异常情况时,将异常信息封装为异常对象,然后throw。
3、try出现在方法体中,它自身是一个代码块,表示尝试执行代码块的语句。如果在执行过程中有某条语句抛出异常,那么代码块后面的语句将不被执行。
4、catch出现在try代码块的后面,自身也是一个代码块,用于捕获异常try代码块中可能抛出的异常。catch关键字后面紧接着它能捕获的异常类型,所有异常类型的子类异常也能被捕获。
演示实例
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package book.exception;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 抛出异常和声明异常
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author joe
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class ThrowAndThrows {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 计算一个数字的平方根
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param nStr 以字符串的形式提供数字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回平方根
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @throws Exception 当用户输入的字符串为空,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 或者字符串无法转换成数字,或者转换成的数字小于0,都会抛出异常
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static double sqrt(String nStr) throws Exception {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (nStr == null) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//用trow关键字抛出异常,当异常被抛出时,程序会跳出该方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("输入的字符不能为空!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double n = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n = Double.parseDouble(nStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch(NumberFormatException e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将parseDouble方法可能抛出的异常NumberFormatException捕获,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//然后将捕获的异常重新封装并输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("输入的字符串必须能够转化成数字!", e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (n < 0 ){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("输入的字符串转化成的数字必须大于0!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return Math.sqrt(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) throws Exception {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ThrowAndThrows.sqrt("-124.56");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch(Exception e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将sqrt方法声明的可能抛出的Exception异常捕获
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//打印捕获的异常的堆栈信息,从堆栈信息中可以发现异常发生的位置和原因
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("Got a Exception:" + e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw e; //不做进一步处理,将异常向外抛出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将sqrt声明了可能会抛出的异常向外抛出,必须在方法声明中使用throws
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ThrowAndThrows.sqrt("-124.56");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序输出结果:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Got a Exception:输入的字符串转化成的数字必须大于0!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
java.lang.Exception: 输入的字符串转化成的数字必须大于0!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.sqrt(ThrowAndThrows.java:30)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.main(ThrowAndThrows.java:37)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Exception in thread "main" java.lang.Exception: 输入的字符串转化成的数字必须大于0!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.sqrt(ThrowAndThrows.java:30)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.main(ThrowAndThrows.java:37)
源码分析:
在main方法里,由于sqrt方法的声明中有throws关键字,所以,在调用该方法时,必须对throws后面声明的异常进行处置,处置的方法有两种:
(1)main方法处理该异常,使用try.....catch语句,将可能会出现的异常的代码放在try块内,将处理异常的代码放在catch块内,并指明catch能够捕获的异常的类型,当异常被捕获时,执行catch块内的语句。
(2)main方法不处理该异常,将异常向外层程序抛出。在方法声明中使用throws关键字抛出异常,方法体中不需要使用try...catch语句。
异常类Exception的getMessage方法用来获取一场的描述信息,printStackTrace方法用来打印异常的堆栈信息,通过堆栈信息能够查明异常发生的原因和位置,在调试的时候常用。
Java异常处理主要通过5个关键字控制:try、catch、throw、throws和finally。try的意思是试试它所包含的代码段中是否会发生异常;而catch当有异常时抓住它,并进行相应的处理,使程序不受异常的影响而继续执行下去;throw是在程序中明确引发异常;throws的作用是如果一个方法可以引发异常,而它本身并不对该异常处理,那么它必须将这个异常抛给调用它的方法;finally是无论发不发生异常都要被执行的代码。
关键字:throw,throws,try和catch的用法如下:
1、throws出现在方法的声明中,表示该方法可能会抛出的异常,允许throws后面跟着多个异常类型。
2、throw出现在方法体中,用于抛出异常。当方法在执行过程中遇到异常情况时,将异常信息封装为异常对象,然后throw。
3、try出现在方法体中,它自身是一个代码块,表示尝试执行代码块的语句。如果在执行过程中有某条语句抛出异常,那么代码块后面的语句将不被执行。
4、catch出现在try代码块的后面,自身也是一个代码块,用于捕获异常try代码块中可能抛出的异常。catch关键字后面紧接着它能捕获的异常类型,所有异常类型的子类异常也能被捕获。
演示实例
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
package book.exception;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 抛出异常和声明异常
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @author joe
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
*
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class ThrowAndThrows {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 计算一个数字的平方根
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param nStr 以字符串的形式提供数字
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @return 返回平方根
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @throws Exception 当用户输入的字符串为空,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* 或者字符串无法转换成数字,或者转换成的数字小于0,都会抛出异常
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static double sqrt(String nStr) throws Exception {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (nStr == null) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//用trow关键字抛出异常,当异常被抛出时,程序会跳出该方法
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("输入的字符不能为空!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
double n = 0;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
n = Double.parseDouble(nStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch(NumberFormatException e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将parseDouble方法可能抛出的异常NumberFormatException捕获,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//然后将捕获的异常重新封装并输出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("输入的字符串必须能够转化成数字!", e);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (n < 0 ){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw new Exception("输入的字符串转化成的数字必须大于0!");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return Math.sqrt(n);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) throws Exception {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
try{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ThrowAndThrows.sqrt("-124.56");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch(Exception e) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将sqrt方法声明的可能抛出的Exception异常捕获
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//打印捕获的异常的堆栈信息,从堆栈信息中可以发现异常发生的位置和原因
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("Got a Exception:" + e.getMessage());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
e.printStackTrace();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
throw e; //不做进一步处理,将异常向外抛出
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//将sqrt声明了可能会抛出的异常向外抛出,必须在方法声明中使用throws
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ThrowAndThrows.sqrt("-124.56");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
程序输出结果:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Got a Exception:输入的字符串转化成的数字必须大于0!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
java.lang.Exception: 输入的字符串转化成的数字必须大于0!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.sqrt(ThrowAndThrows.java:30)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.main(ThrowAndThrows.java:37)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Exception in thread "main" java.lang.Exception: 输入的字符串转化成的数字必须大于0!
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.sqrt(ThrowAndThrows.java:30)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
at book.exception.ThrowAndThrows.main(ThrowAndThrows.java:37)
源码分析:
在main方法里,由于sqrt方法的声明中有throws关键字,所以,在调用该方法时,必须对throws后面声明的异常进行处置,处置的方法有两种:
(1)main方法处理该异常,使用try.....catch语句,将可能会出现的异常的代码放在try块内,将处理异常的代码放在catch块内,并指明catch能够捕获的异常的类型,当异常被捕获时,执行catch块内的语句。
(2)main方法不处理该异常,将异常向外层程序抛出。在方法声明中使用throws关键字抛出异常,方法体中不需要使用try...catch语句。
异常类Exception的getMessage方法用来获取一场的描述信息,printStackTrace方法用来打印异常的堆栈信息,通过堆栈信息能够查明异常发生的原因和位置,在调试的时候常用。
相关文章推荐
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- JAVA基础再回首(十九)——异常的概述、Try…Catch、多异常处理、Throws、throw、finally、自定义异常及异常的注意事项
- Java的异常处理:try-catch-finally throws throw
- Java语言如何进行异常处理,关键字:throws、throw、try、catch、finally分别如何使用?
- JAVA 语言如何进行异常处理,关键字:throws,throw,try,catch,finally 分别代表什么意义?在 try 块中可以抛出异常吗?
- Java异常处理综合例子(try、catch、finally、throws、throw) .
- Java异常处理之throw, throws,try和catch
- 转载:Java的异常处理机制:try、catch、finally、throw、throws
- java异常处理之throw, throws,try和catch
- java异常处理之throw, throws,try和catch
- JAVA之旅(十)——异常的概述,Try-Catch,异常声明Throws,多异常处理,自定义异常,Throw和Throws的区别
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗
- JAVA之旅(十)——异常的概述,Try-Catch,异常声明Throws,多异常处理,自定义异常,Throw和Throws的区别
- 关于java异常处理的几个关键字 try catch/throw/throws
- java异常处理之throw, throws,try和catch
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意思?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?
- 【Java面试题】22 JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- Java异常处理综合例子(try、catch、finally、throws、throw)
- 关于java异常处理的几个关键字 try catch/throw/throws