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

自己用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())));
    
   作为菜鸟的我百思不得其解·自己弄了一下午了!
阅读 8.6k
6 个回答

你应该试一下用try-catch

调用需要抛出异常的方法用try catch 包起来 然后选择处理或者继续往外抛,这样控制台才能打印出到底是异常信息吧

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

新手上路,请多包涵

用try/catch包裹住throw的异常才能捕获异常,并且catch中打印异常才能在控制台看见

public void start(Stage primaryStage) throws Exception{}

你的这个方法你用了throws Exception抛出异常,当你调用这个方法时,必须把这个方法放在try{}catch里捕获,你直接调用这个方法不用try{}catch,当这个方法发生错误时,就会报你出现的那个错误。
另外try{}catch是为了捕获别人的异常,throws是把异常抛给别人

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题