2016-11-05 2 views
-4

Мне было интересно, есть ли кто-то, кто может помочь мне с этими двумя функциями отладки в Java, включая заявления try/catch/throw. Я не могу понять, как отлаживать любое задание, работающее в Zip-файле NetBeans.Как отлаживать попытки и блокировать блоки?

Все или любая помощь приветствуется. Спасибо.

Назначение 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 никогда не войдет в блок catch FileNotFoundException. –

ответ

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, и исправьте его. Подсказка: самое конкретное исключение должно быть первым.

Второй пример не будет скомпилирован. Вам нужно сделать компиляцию, выбросив исключение. Если вы не знаете, как это сделать, посмотрите пример.

Смежные вопросы