-4
我想知道是否有人能夠幫助我在Java中使用這兩個調試任務,涉及try/catch/throw語句。我似乎無法弄清楚如何調試在NetBeans Zip文件中工作的任務。如何調試try和catch塊?
所有或任何幫助表示讚賞。 謝謝。
作業1:
package debugmeone;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/*
* This file requires debugging. This is a partial file to read a text file
* with simple error checking. If the file is not found (you are testing this)
* a FileNotFoundException should be thrown. The second catch statement is
* producing an error (red stop sign). Why? Your job is to have both
* Exception and FileNotFoundException in this file. Do not remove either one
* of them. Don't create the file accountrecords.txt; you are testing for
* a file not found condition so there is no need to create the file.
*
* The output should be:
*
* run:
* Error - File Not Found: accountrecords.txt
* Java Result: 100
* BUILD SUCCESSFUL (total time: 0 seconds)
*/
public class ReadTextFile {
private Scanner input; // Ignore the hint given by NetBeans
public void openFile() {
try
{
input = new Scanner(new File("accountrecords.txt"));
}
catch(Exception e)
{
System.out.println("Something bad just happened here.");
System.exit(707);
}
// Debug this line; what should you do to solve this error message?
// Carefully read the error message provided by the IDE
catch(FileNotFoundException fnfe)
{
System.out.println("Error - File Not Found: accountrecords.txt");
System.exit(100);
}
}
}
作業2:
package debugmetwo;
/*
* You will need to debug this file.
*
* The output should be:
*
* run:
* There is a problem with the Eagle!
* Java Result: 9999
* BUILD SUCCESSFUL (total time: 0 seconds)
*/
public class ThrowEagleExceptionTest {
public static void main(String[] args) {
try {
EagleLanding();
} catch (EagleLandingException badEagle) {
System.out.printf("%s\n", badEagle.getMessage());
System.exit(9999);
}
}
private static void EagleLanding {
EagleLandingException("There is a problem with the Eagle!");
}
}
恩,你有什麼具體問題嗎?你有什麼嘗試?您遇到什麼問題?建議:1)找到一個Netbeans調試器教程(如[this](http://www.cs.columbia.edu/~cmurphy/summer2008/1007/netbeans/7_debugging.html)),2)在調試器中打開你的代碼,3)在'main()'中設置一個斷點,4)在調試器中跟蹤你的代碼。 5)跟蹤「成功」和「錯誤」代碼路徑。 – paulsm4
您的文章沒有描述您有什麼問題... –
在您的ReadTextFile類中,openFile方法永遠不會進入FileNotFoundException catch塊。 –