`
wlh269
  • 浏览: 453107 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

try--catch--finally

    博客分类:
  • J2SE
阅读更多

a.try和catch总有一个要执行;
b.finally总在try或者catch之后执行;
c.某一层捕获了异常,这一层的try代码区中出现异常之后的代码将不被执行
import java.io.File;
import java.io.FileNotFoundException;

public class TestException {

	public static void main(String[] args) {
	   new AA().test1();
	}
}

class AA {

	public int test1(){
		try {
			int j= new AA().test2();
			System.out.println("try  of ...test1");
			return j;
		} catch (Exception e) {
			System.out.println("catch  of ...test1");
			e.printStackTrace();
			return 0;
		} finally {
			System.out.println("finally of ...test1");
		}
	}
	public int test2() {
		try {
			int i = 2 / 0;
			System.out.println("try  of ...test2");//该层捕获了异常,则在异常代码之后的代码将不被执行
			return i;
			
		} catch (Exception e) {
			System.out.println("catch  of ...test2");
			e.printStackTrace();
			return 0;
		} finally {
			System.out.println("finally  of ...test2");
		}
		
	}
}



输出:
catch  of ...test2
finally  of ...test2
try  of ...test1
finally of ...test1

import java.io.File;
import java.io.FileNotFoundException;

public class TestException {

	public static void main(String[] args) {
	   new AA().test1();
	}
}

class AA {

	public int test1(){
		try {
			//该层捕获了异常,则在异常代码之后的代码将不被执行
			int j= new AA().test2();
			System.out.println("try  of ...test1");
			return j;
		} catch (Exception e) {
			System.out.println("catch  of ...test1");
			e.printStackTrace();
			return 0;
		} finally {
			System.out.println("finally of ...test1");
		}
	}
	public int test2() {
		try {
			int i = 2 / 0;
			System.out.println("try  of ...test2");
			return i;
			
		} /*catch (Exception e) {
			System.out.println("catch  of ...test2");
			e.printStackTrace();
			return 0;
		}*/ finally {
			System.out.println("finally  of ...test2");
		}
		
	}
}


输出:
finally  of ...test2
catch  of ...test1
finally of ...test1
分享到:
评论

相关推荐

    try-catch-finally捕获异常

    try-catch-finally捕获异常 try-catch-finally语句是Java语言中一种常用的异常处理机制,当程序在执行过程中出现异常时,可以使用try-catch-finally语句来捕获和处理异常。下面将详细介绍try-catch-finally语句的...

    try-catch-finally-return-in-finally.rar_return

    标题 "try-catch-finally-return-in-finally.rar_return" 提到了 `try-catch-finally` 结构与 `return` 语句的交互,这涉及到程序执行流程的关键方面。 `try` 块是用来包含可能会抛出异常的代码。如果在 `try` 块中...

    使用try-catch-finally处理异常

    你还可以在`try-catch-finally`结构内部嵌套另一个`try-catch-finally`结构,以处理更复杂的情况。这种方式使得异常处理更加灵活,能够针对不同层次的异常进行分别处理。 6. **throw和throws关键字** `throw`...

    try_catch_finally异常处理java

    本文将深入探讨Java中的`try-catch-finally`语句块,以及它们在异常处理中的作用。 首先,`try`块是异常处理的起点。在这个代码段中,我们通常会放置可能会抛出异常的代码。当Java执行到可能抛出异常的代码行时,...

    异常+异常处理+try-catch-finally+教程

    异常+异常处理+try-catch-finally+教程 - 本资源是一个异常处理的教程,教你如何用Java的try-catch-finally语句处理异常,包括抛出和捕获异常,自定义异常,多个异常,finally块等。

    try-finally-throw-in-finally.rar_Called

    标题中的"try-finally-throw-in-finally.rar_Called"暗示我们将讨论与异常处理相关的内容,特别是`try...finally`块中`throw`语句的行为。 `try...finally`结构是用来处理可能出现错误的代码段的,`try`块包含可能...

    try-catch的用法和含义

    ### try-catch 的用法与含义详解 在编程过程中,异常处理是确保程序稳定性和健壮性的关键环节之一。`try-catch` 结构作为异常处理的核心机制,在各种编程语言中广泛采用,如 Java、C# 和 JavaScript 等。本文将深入...

    try-catch用法

    3. **资源清理**:如果 `try` 块中使用了需要关闭的资源(如文件流),应使用 `finally` 块确保资源得到正确的释放。 4. **异常重新抛出**:在某些情况下,可能需要在捕获异常后进行一些处理,然后再重新抛出异常。...

    Java try-with-resource语法使用解析

    其次,try-with-resource语法糖会在编译时生成一个隐含的finally块,在finally块中调用资源的close方法以关闭资源。 最后,try-with-resource语法糖可以自动关闭资源, 无需开发者手动编写关闭资源的代码。 try-...

    try-catch-finally.js:一个很小的库,用于在JavaScript中更灵活地捕获错误

    try-catch-finally.js 843字节库,可在JavaScript中更灵活地捕获错误。内容注意事项按名称捕获可能不起作用按类型捕获在跨框架/过程中不起作用错误被消耗测验 安装 浏览器< script src =" try-catch-finally.js ...

    no-try:通过删除try-catch-finally块来清理代码! :rocket:

    :rocket: 不试 :rocket: 删除那些难看的try-catch-finally块,清理代码库! :smiling_face_with_heart-eyes: 关于在可能期望方法throw的代码库中工作可能会导致逻辑包裹在try-catch块中的情况。 它还导致其他代码...

    try 与 catch finally关键字

    Java的 try 与 catch finally关键字的使用

    Java如何优雅地关闭资源try-with-resource及其异常抑制

    在 try-with-resource 语法中,如果在关闭资源时抛出异常,那么这个异常将被抑制,直到当前 try-catch 代码块执行完毕后,才会被抛出。这使得我们可以更好地处理异常。 try-with-resource 语法和异常抑制机制是 ...

    try-catch-finally的说明和使用

    学习Java异常必要了解try_catch_finally的说明和使用讲解注意事项

    java基础知识-try-catch的使用

    try块中包含可能会产生异常的代码,当try块内的代码发生异常时,程序...另外,finally块是可选的,它会在try-catch块执行完毕后始终被执行,无论是否发生异常。finally块通常用于释放资源、关闭文件等必要的清理工作。

    java try…catch嵌套捕获异常的实例

    在这个结构中,`try`块包含可能会抛出异常的代码,每个`catch`块分别捕获不同类型的异常,`finally`块则包含需要无论发生何种情况都必须执行的代码,如资源清理。 接下来,我们来探讨嵌套的`try-catch`块。当一个`...

    谈谈Java中try-catch-finally中的return语句

    在Java编程语言中,`try-catch-finally`语句块是异常处理的关键构造,它允许程序员优雅地处理可能出现的异常情况。`return`语句则用于从方法中返回一个值或者结束方法的执行。当这两者结合在一起时,就会出现一些...

    try-catch-finally执行顺序验证

    try-catch-finally执行顺序验证(左边是.java文件,右边是.class文件) 提示: try、catch块内的return操作编译后会变成把return的值保存到变量var的操作。 总结: try、catch块内的return操作编译后会变成把return的值...

    375.373.JAVA基础教程_异常处理-处理异常:try-catch方式(375).rar

    异常处理有五个关键字:try、catch、finally、throw和throws。在这其中,try-catch是最基本的结构,用于捕获并处理异常。 1. **try**:try块包含可能会抛出异常的代码。如果try块中的代码抛出了一个异常,那么控制...

    java try…catch捕获异常的实例

    即使在`try`或`catch`块中有`return`语句,`finally`块中的代码也会被执行: ```java try { // 代码 } catch (ExceptionType1 e1) { // 处理 } catch (ExceptionType2 e2) { // 处理 } finally { // 清理资源,...

Global site tag (gtag.js) - Google Analytics