在Java中,每个try块后面都必须紧跟catch块吗?

The question is that "Is it necessary that each try block must be followed by a catch block in Java?"

问题是“在Java中,每个try块后面都必须紧跟catch块吗?”

The answer is "No, it is not mandatory that each try block must be followed by a catch block in Java."

答案是“不,在Java中,每个try块后面都没有catch块不是强制性的。”

  • After try block, we can use either "catch" block or "finally" block.

    在try块之后,我们可以使用“ catch”块或“ finally”块。

  • Generally, thrown exceptions should be declared in the thrown clause of the method.

    通常,应在方法的thrown子句中声明抛出的异常。

  • To understand the try-catch block, we will discuss three cases:

    为了理解try-catch块,我们将讨论三种情况:

    1. What will happen, if each try block must be followed by a catch block?
    2. What will happen, if each try block must be followed by a finally block?
    3. What will happen, if each try block must be followed by both catch and finally block?

In the few steps, we will explore each of the above cases one by one with the help of an example,

在接下来的几个步骤中,我们将借助一个示例逐一探讨上述每种情况,

1)每个try块后面都有一个catch块 (1) Each try block is followed by a catch block)

Example:

例:

// Java program to demonstrate the example of
// try-catch block hierarchy 

public class TryCatchBlock {
    public static void main(String[] args) {

        try {
            int i1 = 10;
            int i2 = 0;
            
            int result = i1 / i2;
            
            System.out.println("The divison of i1,i2 is" + result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
}

Output

输出量

java.lang.ArithmeticException: / by zero
        at TryCatchBlock.main(TryCatchBlock.java:8)

2)每个try块后面都有一个finally块 (2) Each try block is followed by a finally block)

Example:

例:

// Java program to demonstrate the example of
// try-finally block hierarchy 	

public class TryFinallyBlock {
    public static void main(String[] args) {
        
        try {
            int i1 = 10;
            int i2 = 0;
            
            int result = i1 / i2;
            
            System.out.println("The divison of i1,i2 is" + result);
        } finally {
            System.out.print("Code which must be executed :" + " ");
            System.out.println("Whether Exception throw or not throw");
        }
        
    }
}

Output

输出量

Code which must be executed : Whether Exception throw or not throw

Exception in thread "main" java.lang.ArithmeticException: / by zero
	at TryFinallyBlock.main(TryFinallyBlock.java:11)

3)每个try块后面都有catch和finally块 (3) Each try block is followed by both catch and finally block)

Example:

例:

// Java program to demonstrate the example of
// try-catch-finally block hierarchy 

public class TryCatchFinallyBlock {
    public static void main(String[] args) {
        int i1 = 10;
        int i2 = 0;
        
        try {
            int result = i1 / i2;
        
            System.out.println("The divison of i1,i2 is" + result);
        } catch (Exception ex) {
            int result = i1 + i2;
            System.out.println("The addition of i1,i2 is" + " " + result);
        } finally {
            System.out.print("Code which must be executed :" + " ");
            System.out.println("Whether Exception throw or not throw");
        }
    }
}

Output

输出量

The addition of i1,i2 is 10
Code which must be executed : Whether Exception throw or not throw

The combination of try, catch and finally given below are valid and we have seen with the help of an example given above,

下面给出的try,catch和final的组合是有效的,并且我们已经借助上面给出的示例进行了查看,

  • try-catch block

    尝试捕获块

  • try-catch-finally block

    try-catch-finally块

  • try-finally block

    最后尝试块

翻译自: https://www.includehelp.com/java/is-it-necessary-that-each-try-block-must-be-followed-by-a-catch-block-in-java.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值