Java catch Fildnot发现异常e和一个try方法
我有这个问题,我不知道编码有什么问题Java catch Fildnot发现异常e和一个try方法,java,exception,generics,Java,Exception,Generics,我有这个问题,我不知道编码有什么问题 catch (FilenotFoundException e){ system.out.println("File not found"); } try { FileReader freader = new FileReader("MyFile.txt"); } } 它问的是错误是什么??我想可能是e没有大写,原因是?一个try{}块后面应该跟
catch (FilenotFoundException e){
system.out.println("File not found");
}
try
{
FileReader freader = new FileReader("MyFile.txt");
}
}
它问的是错误是什么??我想可能是e没有大写,原因是?一个try{}块后面应该跟一个catch{}块,或者最后一个{}块,你把它颠倒了
这样使用:-
try {
FileReader freader = new FileReader("MyFile.txt");
} catch (FileNotFoundException e){
System.out.println("File not found");
}
根据Java命名约定:-
类名以大写字母开头,所有后续单词也以大写字母开头。因此,FilenotFoundException应该是FilenotFoundException
而且,系统应该是->系统
这应该是另一种方式。试着接住
catch{}块跟随try{}块,而不是相反
此外,FilenotFoundException应该是FilenotFoundException。我怀疑它是否能用另一种拼法编译。正如@Rohit Jain的回答所示,system vs.system也是如此。自Java 7以来:
try( FileReader freader = new FileReader("MyFile.txt"))
{
使用freader
捕捉块应跟随try
try {
//code that exception might occur
}
catch(Exception ex) {
//catch the exception here.
}
try块后面应该跟catch或finally
try {
//code that exception might occur
}
finally {
//close your resources here
}
try {
//code that exception might occur
}
catch(Exception ex) {
//catch the exception here.
}
try {
//code that exception might occur
}
finally {
//close your resources here
}