try {
...;
}
catch() {
...;
}
catch() {
...;
}
finally {
...;
}
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
String everything = sb.toString();
}
catch(IOException e) {
e.printStackTrace();
System.out.println("IO problem");
}
finally {
br.close();
}
public class Test
{
public static void main(String[] args)
{
Battery aBattery = new Battery();
aBattery.chargeBattery(0.5);
aBattery.useBattery(-0.5);
}
}
class Battery
{
/**
* increase battery
*/
public void chargeBattery(double p)
{
// power <= 1
if (this.power + p < 1.) {
this.power = this.power + p;
}
else {
this.power = 1.;
}
}
/**
* consume battery
*/
public boolean useBattery(double p)
{
try {
test(p);
}
catch(Exception e) {
System.out.println("catch Exception");
System.out.println(e.getMessage());
p = 0.0;
}
if (this.power >= p) {
this.power = this.power - p;
return true;
}
else {
this.power = 0.0;
return false;
}
}
/**
* test usage
*/
private void test(double p) throws Exception // I just throw, don't handle
{
if (p < 0) {
Exception e = new Exception("p must be positive");
throw e;
}
}
private double power = 0.0; // percentage of battery
}
useBattery()表示使用电池操作。useBattery()方法中有一个参数,表示使用的电量。我们使用test()方法测试该参数。如果该参数为负数,那么我们认为有异常,并抛出。
在test中,当有异常发生时(p < 0),我们创建一个Exception对象e,并用一个字符串作为参数。字符串中包含有异常相关的信息,该参数不是必需的。使用throw将该Exception对象抛出。
我们在useBattery()中有异常处理器。由于test()方法不直接处理它产生的异常,而是将该异常抛给上层的useBattery(),所以在test()的定义中,我们需要throws Exception来说明。
(假设异常处理器并不是位于useBattery()中,而是在更上层的main()方法中,我们也要在useBattery()的定义中增加throws Exception。)
在catch中,我们使用getMessage()方法提取其异常中包含的信息。上述程序的运行结果如下:
catch Exception p must be positive
class BatteryUsageException extends Exception
{
public BatteryUsageException() {}
public BatteryUsageException(String msg) {
super(msg);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有