Anthony Gayflor Anthony Gayflor - 1 year ago 61
Java Question

Syntax error in Try & Catch

I am having issues completing a Try and Catch. I keep getting a syntax error asking for "(" "{" ";" tokens where they are not needed at. Searched all over the for answer and I have yet to come to a conclusion as to why I keep getting this error. Here is my code

import javax.swing.JOptionPane;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuBar;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebHistory;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Browser extends Application{

final Button Back = new Button("<-");
final Button Forward = new Button ("->");
final Button Home = new Button("Home");
final Button Reload = new Button("r");
final MenuBar menuBar = new MenuBar();
final TextField txt = new TextField();

public void start(Stage stage) throws Exception{
VBox hBox = new VBox();
WebView wv = new WebView();
WebEngine engine = wv.getEngine();
WebHistory wh = engine.getHistory();

txt.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent event){
}catch(Exception e){
showError("Unable to load site");
hBox.getChildren().setAll(txt, wv);

Scene scene = new Scene(hBox, 800, 600);
stage.setTitle("Anthony Gayflor-CS260 OOP");


private void showError(String errorMessage){
JOptionPane.showMessageDialog(this, errorMessage, "Error", JOptionPane.ERROR_MESSAGE);

public static void main(String[] args){

when I compile it, the error I get is

Caused by: java.lang.Error: Unresolved compilation problems:
Syntax error, insert "}" to complete ClassBody
Syntax error, insert ")" to complete MethodInvocation
Syntax error, insert ";" to complete Statement
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
Syntax error on tokens, delete these tokens

at Browser.start(

Answer Source

In addition to Jens' answer, you can use lambda in Java 8, because EventHandler is a functional interface. Then your code will look like:

txt.setOnAction(event -> {
    try {
    }catch (Exception e) {
        showError("Unable to load site");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download