Java - Try / Catch - 如果成功,则访问try块中的变量

时间:2013-09-26 05:49:29

标签: java try-catch

说我在某处有以下代码块:

while(condition){
    try {
        String twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

\\do stuff with twoStr

我无法访问try / catch语句之外的字符串,但我不想提示用户输入另外两个单词,以防他搞砸并输入单个单词。如何访问在try块中初始化的值?

8 个答案:

答案 0 :(得分:5)

字符串应该在try / catch语句之外定义:

while(condition){
    String twoStr = null;

    try {
        twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
     } catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

     //do stuff with twoStr

通过这种方式,try / catch语句及其外部的代码都将看到变量。您应该阅读scopes以更好地了解这一点。

答案 1 :(得分:2)

当您在string块中定义try catch时,它只对该块是本地的,并且无法在外部访问。在try catch块之外定义String变量,如下所示 -

while(condition){
String twoStr;
try {
    twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
    //do stuff 
 }catch (IOException e) {
    System.out.println("Sorry your name is bad.");
 }
.....

答案 2 :(得分:2)

更改twoStr

的范围
while(condition){
String twoStr= null;
    try {
         twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your name is bad.");
     }

\\do stuff with twoStr

答案 3 :(得分:2)

在try catch之外声明String。如果在try catch中声明一个变量,那么变量的范围仅适用于try catch内部。所以你不能在外面使用它。

 while(condition){
     String twoStr =null;
    try {
        twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
       //do stuff like splitting and taking an index which may cause out of bound exception
       }
    catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

答案 4 :(得分:0)

String twoStr = null;
while(condition){
     try {
        twoStr = JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your name is bad.");
     }

\\do stuff with twoStr

答案 5 :(得分:0)

twoString是在try-catch块的上下文中定义的,因此无法访问它,而是需要在try-catch块之前定义变量...

while(condition){
    String twoStr = null;
    try {
        twoStr = JOptionPane.showInputDialog("Enter two words sep by a space.");
        //do stuff like splitting and taking an index which may cause out of bound exception
    } catch (IOException e) {
        System.out.println("Sorry your name is bad.");
    }

    // You now have access to twoStr

答案 6 :(得分:0)

只需在try-block之外声明:

String twoStr = null;
while(condition){
    try {
        twoStr= JOptionPane.showInputDialog("Enter two words sep by a space.");
        \\do stuff like splitting and taking an index which may cause out of bound exception
    catch (IOException e) {
        System.out.println("Sorry your words are terrible.");
     }

\\do stuff with twoStr

答案 7 :(得分:0)

您无法访问try catch块之外的字符串变量String twoStr,因为在try / catch块中声明的变量范围不在包含块的范围内,原因与所有其他变量声明的原因相同它们发生的范围是本地的。 您需要从try catch块中声明该变量。