---
title: "finally语句的妙用"
author: "wangyun"
date: "2008-06-03T21:53:25Z"
tags: []
slug: "finally"
draft: false
lastmod: "2008-06-03T21:53:25Z"
id: 199894
url: "https://wangyun.iteye.com/blog/199894"
---

在Java 中finally 语句只用于try-catch 语句之后，用于清理的目的。但是我们可以通过finlly 语句的妙用达到goto 语句的效果（java 中没有goto 语句，只有goto 这个保留字。）

代码如下：
/**
 *  通过finally 语句的妙用
 *  在Java 中达到goto语句的效果
 */
public class Goto {
	
	static int count = 0;
	
	/**
	 * @param args
	 */
	public static void main(String args) {
		// TODO Auto-generated method stub
		while(true) {
			try {
				if(count++ == 0)
					throw new Exception();
				System.out.println("No Exception.");
			}catch(Exception e) {
				System.out.println("Caught Exception!");	
			}finally {
				System.out.println("In finally clause.");
				if(count == 2)
					break;
			}
		}
		System.out.println("While end.");
	}
}

这段代码同时也给我们另外的一种思路：在Java 中我们如何回到异常抛出的地点。