Java-Try/Catch-Access Try块中的变量(如果成功)
假设我在某处有以下代码块:Java-Try/Catch-Access Try块中的变量(如果成功),java,try-catch,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) {
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块中初始化的此值?字符串应在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语句以及它外部的代码都将看到该变量。您应该继续阅读以更好地理解这一点。当您在
try catch
块内定义字符串时,它仅对该块是本地的,在外部无法访问。在try-catch块外部定义字符串变量,如下所示-
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
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.");
}
.....
更改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
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
只需在try块之外声明它:
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
在try-catch之外声明字符串。如果在try-catch内声明变量,则该变量的作用域仅用于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.");
}
无法在try-catch块外部访问字符串变量string twoStr
,因为try/catch块中声明的变量的范围不在包含块的范围内,原因与所有其他变量声明都是发生在其中的范围的局部变量的原因相同。
您需要从try-catch块中声明该变量。是的,还修复了注释,谢谢:)现在没有编译器错误,仍然无法编译;)(不是为了OP至少需要它…)嗯,嗯,提示,在try catch
之后尝试使用twoStr
值)我可能会吹毛求疵,并指出这不会编译;)我会添加“为什么”并修复最后一行,这样它就接近编译了;)@MadProgrammer:LOL:)下次我在复制OP的代码时会注意:)这是一个局部变量,请将声明移出try/catch。