已经抛出异常为什么还是未报告异常?

自己用javafx练习组合框时写了一个小程序,使用到了文件输入,但是调用需要抛出异常的函数时,明明已经抛出了,为什么还提示未报告异常?


import javafx.application.Application;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import java.util.*;

public class ComboBoxDemo extends Application
{
    private String[] flagTitle = {"Canada","America","China"};
    private ImageView[] flagImage = {new ImageView("card/canada.gif"),new ImageView("card/america.gif"),new ImageView("card/china")};
    private String[] flagDescription = {"Canada.txt","America.txt","China.txt"};
    private DescriptionPane descriptionPane = new DescriptionPane();
    private ComboBox<String> cbo = new ComboBox<>();
    
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        setDisplay(0);
        BorderPane pane = new BorderPane();
        
        BorderPane paneForComboBox = new BorderPane();
        paneForComboBox.setLeft(new Label("Select a Country"));
        paneForComboBox.setCenter(cbo);
        pane.setTop(paneForComboBox);
        
        cbo.setPrefWidth(400);
        cbo.setValue("Canada");
        
        ObservableList<String> item = FXCollections.observableArrayList(flagTitle);
        cbo.getItems().addAll(item);
        pane.setCenter(descriptionPane);
            
        ***cbo.setOnAction(e->setDisplay(item.indexOf(cbo.getValue())));***
        
        Scene scene = new Scene(pane,400,500);
        primaryStage.setTitle("ComboBoxDemo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public void setDisplay(int index)  throws Exception
    {
        descriptionPane.setTitle(flagDescription[index]);
        descriptionPane.setImage(flagImage[index]);
        descriptionPane.setDescription(flagDescription[index]);
    }
}

import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import java.util.Scanner;
import java.io.File;
public class DescriptionPane extends BorderPane
{
    private Label lblImageTitle = new Label();
    private TextArea taDescription = new TextArea();
    
    public DescriptionPane()
    {
        lblImageTitle.setContentDisplay(ContentDisplay.TOP);
        lblImageTitle.setPrefSize(200,200);
        lblImageTitle.setFont(new Font("SansSerif",16));
        taDescription.setFont(new Font("Serif",14));
        
        taDescription.setEditable(false);
        taDescription.setWrapText(true);
        
        ScrollPane scrollPane = new ScrollPane(taDescription);
        
        setLeft(lblImageTitle);
        setCenter(scrollPane);
        setPadding(new Insets(5,5,5,5));
    }
    public void setTitle(String title)
    {
        lblImageTitle.setText(title);
    }
    public void setImage(ImageView icon)
    {
        lblImageTitle.setGraphic(icon);
    }
    public void setDescription(String name)  throws Exception
    {
        File file = new File(name);
        String  description = new Scanner(file).next();
        taDescription.setText(description);
    }
}

D:javaJCComboBoxDemo.java:38: 错误: 未报告的异常错误Exception; 必须对其进行捕获或声明以便抛出

    cbo.setOnAction(e->setDisplay(item.indexOf(cbo.getValue())));
    
   作为菜鸟的我百思不得其解·自己弄了一下午了!
慕哥9229398
浏览 845回答 6
6回答

莫回无

你应该试一下用try-catch

PIPIONE

单单抛的话,并不会捕捉异常,要try catch捕捉抛出的异常进行你的处理

慕运维8079593

public void start(Stage primaryStage) throws Exception{} 你的这个方法你用了throws Exception抛出异常,当你调用这个方法时,必须把这个方法放在try{}catch里捕获,你直接调用这个方法不用try{}catch,当这个方法发生错误时,就会报你出现的那个错误。另外try{}catch是为了捕获别人的异常,throws是把异常抛给别人
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java