ITeye博客

try{ } catch{ } finally{ } 语句的执行流程

ArtShell · 2013-03-15 15:52 · Java SE · 编程语言 · Markdown

今天群里讨论了一个问题:try{ } catch{ } finally{ } 语句的执行流程!

 

在这里做下记录:

 

 

【程序清单1】:

	public static void main(String[] args){
		try {
			System.out.println("try");
		} catch (Exception e) {
			System.out.println("catch");
		} finally {
			System.out.println("finally");
		}
		System.out.println("other");
	}

 

 

 

 

 

【程序清单2】:

public static void main(String[] args){
		String s = null;
		try {
			System.out.println("try");
			s.equals(""); // 让catch 捕获一个 NullPointerException对象;
		} catch (Exception e) {
			System.out.println("catch");
		} finally {
			System.out.println("finally");
		}
		System.out.println("other");
	}

 

 

 

 

【程序清单3】:

public static void main(String[] args) throws Exception{
		String s = null;
		try {
			System.out.println("try");
			s.equals(""); // 让catch 捕获一个 NullPointerException对象;
		} catch (Exception e) {
			System.out.println("catch");
			throw new Exception(e); // 或者 throw e;
		} finally {
			System.out.println("finally");
		}
		System.out.println("other");
	}

 

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。