在try catch中访问变量
我一直在返回menuFont行上遇到编译错误,它说没有变量menuFont。 有人可以告诉我如何解决这个问题。
import java.awt.Font; import java.awt.FontFormatException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class loadFiles { Font getFont(){ try { Font menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } return menuFont; } }
代码的基本问题是Font对象仅在try块的持续时间范围内,因此在方法结束时的return语句中不再可用。 两种选择:
将变量声明移到try块之外:
Font menuFont = null; try { menuFont = Font.createFont(...); } catch (...) { } return menuFont;
或者,在try中return Font.creatFont(...)
,从而避免首先需要变量(显然,在方法结束时return null
)。
你必须在外面声明变量menuFont …
import java.awt.Font; import java.awt.FontFormatException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class loadFiles { Font getFont(){ Font menuFont = null; try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } return menuFont; } }
menuFont
变量作用域位于try块内。 将变量移到其外部。 就像是:
Font getFont(){ Font menuFont = null; try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } return menuFont; }
请注意,如果发生exception,此方法将返回null
字体。
这是因为menuFont不存在于getFont方法的范围内。 在当前作用域/块(花括号对)之外定义的变量是可用的,而在嵌套块中定义的变量则不可用。 您可以通过以下两种方式之一进行更正:
- 将
menuFont
的声明menuFont
上方(在getFont
的范围内)。 因为您预计Font.createFont中存在exception,所以请将该代码保留在try
块中。 - 将
return
语句移动到try
块内。
请参阅最终总是在Java中执行的答案? 关于try / catch / finally的更多细微之处。
那是因为你的menuFont
变量的范围。 你在try
声明它,这意味着它不能从任何地方访问,而是在这两个大括号内。 如果您希望能够在catch
中或try
之外的任何位置访问它,请将代码更改为以下内容:
Font menuFont = null; try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { // you can access menuFont here (too) } //... return menuFont; // <-- note: can be null here
menuFont
不存在于try
块之外,这是编译器试图告诉你的。 相反,在try
块之前声明它,然后尝试在try
块内为其赋值:
Font menuFont; try { Font menuFont = ... } ... return menuFont;
一般来说,我会做以下事情:
import java.awt.Font; import java.awt.FontFormatException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class loadFiles { Font menuFont = null; Font getFont(){ try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } } return menuFont; // could potentially be null! }
并在任何调用此方法的地方检查null
。