简体   繁体   English

如何从 try catch 块外部访问变量?

[英]How can I access a variable from outside the try catch block?

I'm getting a "len cannot be resolved to a variable" error here我在这里收到“len 无法解析为变量”错误

        try {
            byte len = (byte) passLength();
        }
        catch(Exception InputMismatchException) {
            System.out.println("Error, please input a proper number");
        }
        finally {
            String result = passGen(len, chars);
            System.out.println(result);
        }

If the try block encounters an exception, the len variable would be undefined.如果 try 块遇到异常,则len变量将未定义。

If you're sure an exception wouldn't occur, you may initialize a temporary byte variable before the try-catch block.如果您确定不会发生异常,您可以在 try-catch 块之前初始化一个临时字节变量。

byte temp;
try {
            byte len = (byte) passLength();
            temp = len;

 } catch(Exception InputMismatchException) {
            System.out.println("Error, please input a proper number");
 }
 finally {
     String result = passGen(temp, chars);
     System.out.println(result);
 }

Because you have declared len variable inside try block and its scope is in try block itself and is not accessible in finally block.因为您已经在 try 块中声明了 len 变量,并且它的 scope 位于 try 块本身中,并且在 finally 块中不可访问。 Try declaring len outside the try block.尝试在 try 块之外声明 len 。 It would work.它会起作用的。

Declare the variable outside the scope of try block.在 try 块的 scope 之外声明变量。 Finally block cannot access the scope of try block. finally 块无法访问 try 块的 scope。

        byte len = 0;
        try {
            len = (byte) passLength();
        }
        catch(Exception InputMismatchException) {
            System.out.println("Error, please input a proper number");
        }
        finally {
            String result = passGen(len, chars);
            System.out.println(result);
        }

Scope of a variable: As a general rule, variables that are defined within a block are not accessible outside that block.变量的 Scope:作为一般规则,在块内定义的变量不能在该块外访问。

Lifetime of a variable: Once a variable loses it scope, Garbage collector will takes care of destroying the variable/object.变量的生命周期:一旦变量丢失 scope,垃圾收集器将负责销毁变量/对象。 The lifetime of a variable refers to how long the variable exists before it is destroyed.变量的生命周期是指变量在被销毁之前存在的时间。

try {
     byte len = (byte) passLength();
}

In your above example, the variable len is declared inside the try block , its scope is only within the try block and can't be accessed outside the try block.在上面的示例中,变量lentry 块内声明,其 scope 仅在 try 块内,不能在 try 块外访问。

You should declare the len variable even before the try block, so that it can be accessed in the finally block .您甚至应该在 try 块之前声明len变量,以便可以在finally 块中访问它。

byte len = Byte.MIN_VALUE;  //This value is for dummy assignment
try {
     len = (byte) passLength();
} catch(Exception inputMismatchException) { // Avoid using variable name starts with Capitals
     System.out.println("Error, please input a proper number");
} finally {
     String result = passGen(len, chars);
     System.out.println(result);
}

Hope, this will be helpful:)希望,这会有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM