2016-11-05 16 views
-4

私は、try/catch/throw文を使ってJavaでこれらの2つのデバッグ課題を手伝ってくれる人がいるかどうか疑問に思っていました。 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!"); 
    } 
} 
+2

質問がありますか?何を試しましたか?まさにどのような問題が発生しましたか?提案:1)Netbeansデバッガチュートリアル([this](http://www.cs.columbia.edu/~cmurphy/summer2008/1007/netbeans/7_debugging.html)のような)を見つけて、2)デバッガでコードを開く、3) 'main()'にブレークポイントを設定する、4)デバッガでコードをトレースする。 5) "成功"と "エラー"の両方のコードパスをトレースします。 – paulsm4

+1

あなたの投稿は、あなたが持っている問題を記述していません... –

+0

ReadTextFileクラスでは、openFileメソッドは決してFileNotFoundException catchブロックに入りません。 –

答えて

1

あなたはコンパイル時のエラー・メッセージではなく、デバッガが何のためにあるのかであるランタイム1を持っています。読むメッセージは

// Debug this line; what should you do to solve this error message? 
    // Carefully read the error message provided by the IDE 
    catch(FileNotFoundException fnfe) 

です。IDEで提供されているエラーメッセージを読んで、修正してください。ヒント:最も具体的な例外が最初に来なければなりません。

2番目の例でもコンパイルされません。あなたは例外をスローすることによってそれをコンパイルする必要があります。あなたがそれを行う方法がわからない場合は、例を見てください。

関連する問題