Exception相关

创建一个自己的Exception

class SimpleException extends Exception {}

//创建一个自己的Exception 继承 Exception


public class SimpleExceptionDemo {
public void f() throws SimpleException {
System.out.println(
"Throwing SimpleException from f()");
throw new SimpleException ();  //在这个方法中抛出这个自定义的Exception
}
public static void main(String[] args) {
SimpleExceptionDemo sed =
new SimpleExceptionDemo();
try {
sed.f();
} catch(SimpleException e) {    //catch这个Exception 处理结果是打印出Caught it 或者可以打印出这个Exception 如:System.out.println(e);
System.err.println("Caught it!");
}
}
}

 

finally的使用:

class ThreeException extends Exception {}
public class FinallyWorks {
static int count = 0;
public static void main(String[] args) {
while(true) {
try {
// Post-increment is zero first time:
if(count++ == 0)
throw new ThreeException();
System.out.println("No exception");
} catch(ThreeException e) {
System.err.println("ThreeException");
} finally {
System.err.println("In finally clause");
if(count == 2) break; // out of "while"
}
}
}
}

通过该程序我们亦可知道如何应付Java 违例类似C++的违例不允许我们恢复至
违例产生地方的这一事实前面已经讲过若将自己的try 块置入一个循环内就可建立
一个条件只有满足这个条件才能继续程序亦可添加一个static 静态计数器或另一些
设备允许循环在彻底放弃之前尝试数种不同的方法这样一来我们的程序就会变得更加
健壮在各式各样的错误面前更能免疫
输出如下
ThreeException
In finally clause
No exception
In finally clause
显然 无论是否掷出一个违例finally 从句都会执行

 

为什么使用finally

在没有垃圾收集以及自动调用破坏器机制的一种语言中50 finally 显得特别重
要因为程序员可用它担保内存得以正确释放回收无论在try 块内部发生了什么事
情但由于Java 提供了垃圾收集机制所以内存的回收几乎绝对不会成为问题另外Java
里也没有破坏器可供调用既然如此在Java 里什么时候才轮到使用finally 呢
答案在于假如你除了想把内存恢复成原始状态之外还想设置另一些东西finally 就
是必需的例如我们有时需要打开一个文件或者建立一个网络连接或者在屏幕上画一些
东西甚至设置外部世界的一个开关等等如下例所示
//: c10:OnOffSwitch.java
// Why use finally?
class Switch {
boolean state = false;
boolean read() { return state; }
void on() { state = true; }
void off() { state = false; }
}
class OnOffException1 extends Exception {}
class OnOffException2 extends Exception {}
public class OnOffSwitch {
static Switch sw = new Switch();
static void f() throws
OnOffException1, OnOffException2 {}
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions...
f();
sw.off();
} catch(OnOffException1 e) {
System.err.println("OnOffException1");
sw.off();
} catch(OnOffException2 e) {
System.err.println("OnOffException2");
sw.off();
}
}
}

破坏器 Destructor 是构造函数Constructor 的反义词它代表一个特殊的函数一旦某个对象失去用处就肯定
会调用它我们肯定知道在哪里以及何时调用破坏器C++提供了破坏器的自动调用机制但Delphi 公司的Object Pascal 版
本1 及2 却不具备这一能力在这种语言中破坏器的含义与用法都发生了变化

在这里我们的目标是保证main()完成时开关处于关闭状态所以将sw.off()置于try 块
以及每个违例控制器的末尾但产生的一个违例有可能不是在这里捕获的这样便会错过
sw.off() 不过利用finally 来自一个try 块的清除代码只需放在一个地方我们就可以高
枕无忧了不必象上面那样把sw.off()放得到处都是

//: c10:WithFinally.java
// Finally Guarantees cleanup.
public class WithFinally {
static Switch sw = new Switch();
public static void main(String[] args) {
try {
sw.on();
// Code that can throw exceptions...
OnOffSwitch.f();
} catch(OnOffException1 e) {
System.err.println("OnOffException1");
} catch(OnOffException2 e) {
System.err.println("OnOffException2");
} finally {
sw.off();
}
}
}
在这儿 sw.off()只存在于一个地方无论发生什么事情都肯定会运行它
即使违例不在当前的catch 从句集里捕获finally 都会在违例控制机制转到一个更高级
别的控制器之前得以执行如下所示

//: c10:AlwaysFinally.java
// Finally is always executed.
class FourException extends Exception {}
public class AlwaysFinally {
public static void main(String[] args) {
System.out.println(
"Entering first try block");
try {
System.out.println(
"Entering second try block");
try {
throw new FourException();
} finally {
System.out.println(
"finally in 2nd try block");
}
} catch(FourException e) {
System.err.println(
"Caught FourException in 1st try block");
} finally {
System.err.println(
"finally in 1st try block");
}
}
}
从该程序的输出 我们看到具体发生了什么事情
Entering first try block
Entering second try block
finally in 2nd try block
Caught FourException in 1st try block
finally in 1st try block
若调用了break 和continue 语句finally 语句也会得以执行请注意通过与标签式break
以及标签式continue 的配合finally 便排除了在Java 里使用goto 跳转语句的必要

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值