0

when i was working on a small project of mine, i stumpled upon a weird error the Java compiler gives me, when i want to compile the code. The compilar says that i have an unhandled Interruptedexception. This is because i use Thread.sleep(). Here in my code you can clearly see that i handle the exception, but still the compiler insists that i dont. Instead of doing it this way I also tried doing it by using a try/catch block, but this also did not help. The weird thing also is the compiler does not give me an error if i do buttonpress(); instead of generate.setOnAction(e -> buttonpress()); (generate is just a button). The line java complains about is the last line in my first code block. Here is my code:

@FXML
@Override
public void start(Stage primaryStage) throws InterruptedException, IOException {
    window = primaryStage;
    window.setTitle("RNG");
    Parent root = FXMLLoader.load(getClass().getResource("FXML.fxml"));

    lowerbound = (TextField) root.lookup("#lowerboundinput");
    upperbound = (TextField) root.lookup("#upperboundinput");
    output = (Text) root.lookup("#randomnumberoutput");
    Button generate = (Button) root.lookup("#generatebutton");

    generate.setOnAction(e -> buttonpress());

Here is the buttonpress method:

public static void buttonpress()throws InterruptedException{
    if(!lowerbound.getText().equals("")|| !upperbound.getText().equals("")) {
            RandomNumberGenerator.JavaFXLauncher(lowerbound.getText(), upperbound.getText());
    }else{
        setOutput("EMPTY");
    }
}

and here is the JavaFXLauncher method:

public static void JavaFXLauncher(String firstbound, String secondbound) throws InterruptedException{
    int random;
    try {
        if(inputvalidator(firstbound, secondbound)) {
            random = Integer.parseInt(firstbound) + (int) (Math.random() * ((Integer.parseInt(secondbound) - Integer.parseInt(firstbound)) + 1));
            Main.setOutput(Integer.toString(random));

            TimeUnit.MILLISECONDS.sleep(50);
            //some other code

} catch(NumberFormatException e){
        Main.setLowerbound("Pick smaller number");
        Main.setUpperbound("Integer overflow");
    } catch (InterruptedException f){

    }

}
Hakan
  • 21
  • 4
  • You need to 1) show the complete and full error message and 2) indicate which line(s) are causing it unambiguously. Otherwise this will remain a duplicate of the thousands of similar questions without this information. – Hovercraft Full Of Eels Jul 08 '18 at 00:21
  • The full error message is just java complaining that i have an unhandled exception. The line which is causing it is the last line in the first code block. I also stated the line in my explanation. – Hakan Jul 08 '18 at 00:23
  • Then the compiler is (of course) correct since you're using a lambda expression, one that does not handle the exception at all. – Hovercraft Full Of Eels Jul 08 '18 at 00:24
  • Why is the compiler than not complaining in my second code block? And how should i handle the exception otherwise, like i said using a try/catch block also did not work – Hakan Jul 08 '18 at 00:27
  • Because you're forgetting the lambda and what it does -- it creates an anonymous inner class, and a method called by that class is not handled by your try/catch – Hovercraft Full Of Eels Jul 08 '18 at 00:28
  • Okay, thanks. I did not know about a lambda expression and i also did not find anything when googling before i posted this on stackoverflow. I'll look more into these lambda expressions – Hakan Jul 08 '18 at 00:31
  • You should have removed the `throws InterruptedException` from both `JavaFXLauncher` and `buttonpress` since you did handle the exception in `JavaFXLauncher` and therefore the `throws` clauses should be unnecessary. – fabian Jul 08 '18 at 07:19

0 Answers0