Java异常处理综合例子(try、catch、finally、throws、throw) .
2014-02-12 15:05
781 查看
Java异常处理综合例子(try、catch、finally、throws、throw)
[java]
view plaincopyprint?
package cn.edu.uibe.exp;
class MyExp extends Exception{
private static final long serialVersionUID = 1L;
MyExp(){
super("自定义异常");
}
}
public class Exp2 {
public void f1(int n) {
System.out.println("f1前面");
f2(n);
System.out.println("f1后面");
}
public void f2(int n) {
System.out.println("f2前面");
try{
System.out.println("try的前面");
f3(n);
System.out.println("try的后面"); //发生异常时不会输出
}catch(MyExp e){
System.err.println(e.getMessage());
}finally{
System.out.println("无论是否发生异常");
}
System.out.println("f2后面");
}
public void f3(int n) throws MyExp{
System.out.println("f3前面");
f4(n);
System.out.println("f3后面"); //发生异常时不会输出
}
public void f4(int n) throws MyExp{
System.out.println("f4前面");
if(n<0){
throw new MyExp();
}
System.out.println(n); //发生异常时不会输出
System.out.println("f4后面"); //发生异常时不会输出
}
public static void main(String[] args) {
Exp2 exp = new Exp2();
exp.f1(-4);
}
}
博文转载至: http://blog.csdn.net/microtong
[java]
view plaincopyprint?
package cn.edu.uibe.exp;
class MyExp extends Exception{
private static final long serialVersionUID = 1L;
MyExp(){
super("自定义异常");
}
}
public class Exp2 {
public void f1(int n) {
System.out.println("f1前面");
f2(n);
System.out.println("f1后面");
}
public void f2(int n) {
System.out.println("f2前面");
try{
System.out.println("try的前面");
f3(n);
System.out.println("try的后面"); //发生异常时不会输出
}catch(MyExp e){
System.err.println(e.getMessage());
}finally{
System.out.println("无论是否发生异常");
}
System.out.println("f2后面");
}
public void f3(int n) throws MyExp{
System.out.println("f3前面");
f4(n);
System.out.println("f3后面"); //发生异常时不会输出
}
public void f4(int n) throws MyExp{
System.out.println("f4前面");
if(n<0){
throw new MyExp();
}
System.out.println(n); //发生异常时不会输出
System.out.println("f4后面"); //发生异常时不会输出
}
public static void main(String[] args) {
Exp2 exp = new Exp2();
exp.f1(-4);
}
}
博文转载至: http://blog.csdn.net/microtong
相关文章推荐
- Java异常处理综合例子(try、catch、finally、throws、throw)
- Java异常处理综合例子(try、catch、finally、throws、throw)
- Java语言如何进行异常处理,关键字:throws、throw、try、catch、finally分别如何使用?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?
- 【Java面试题】22 JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- Java异常处理 (try catch finally throw throws exception error)
- JAVA 语言如何进行异常处理,关键字:throws,throw,try,catch,finally 分别代表什么意义?在 try 块中可以抛出异常吗?
- java异常处理:try、catch、throw、throws和finally
- Java的异常处理:try-catch-finally throws throw
- Java的异常处理——try-catch-finally-throw-throws
- Java语言中进行异常处理的关键字具体含义:throws、throw、try、catch、finally
- Java - Java语言如何进行异常处理,关键字:throws、throw、try、catch、finally分别如何使用?
- JAVA基础再回首(十九)——异常的概述、Try…Catch、多异常处理、Throws、throw、finally、自定义异常及异常的注意事项
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意思?
- JAVA语言如何进行异常处理,关键字:throws,throw,try,catch,finally分别代表什么意义?在try块中可以抛出异常吗?
- 转载:Java的异常处理机制:try、catch、finally、throw、throws