Java 访问try-catch中的变量
我一直在返回menuFont行上得到一个编译错误,它说没有变量menuFont。有人能告诉我怎么解决这个问题吗Java 访问try-catch中的变量,java,try-catch,Java,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 getFo
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中返回Font.creatFont(…),这样就避免了首先需要变量(而且,很明显,
在方法末尾返回null
)menuFont
不存在于try
块之外,这正是编译器试图告诉您的。相反,在try
块之前声明它,然后在try
块内尝试为它赋值:
Font menuFont;
try {
Font menuFont = ...
}
...
return 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(){
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
中声明它,这意味着除了在这两个大括号内,它将无法从任何位置访问。如果您希望能够在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
Font-menuFont=null;
试一试{
menuFont=Font.createFont(Font.TRUETYPE\u字体,新文件输入流(“Font.ttf”);
}catch(filenotfounde异常){
//您也可以在此处访问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
。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;
}
请注意,如果出现异常,此方法将返回一个null
字体。在try
块之外声明它
这是因为menuFont不在getFont方法的范围内。在当前范围/块(一对花括号)之外定义的变量可用,而在嵌套块中定义的变量不可用。您可以通过以下两种方式之一更正此问题:
- 将
menuFont
的声明移动到try
上方(在getFont
的范围内)。因为您预期Font.createFont中会出现异常,所以请将该代码保留在try
块中
- 将
return
语句移动到try
块内
有关try/catch/finally的更多细节,请参见的答案