类抛出异常 [英] Class throws exception

查看:123
本文介绍了类抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Netbeans中重构一个大型程序,我有点迷失了。我从来没有非常模块化,但是正在通过实际学习如何做到这一点,我正在努力纠正现在和未来。不幸的是,我无法将一些教程翻译成我的程序的野兽。所以我希望这里有人能帮忙。目前,我正在尝试中断一大段代码,该代码采用特定格式的文件并创建一个表。我知道我需要一个类,并使用它创建一个表对象,但我不确定如何。
我有一个主要获取文件所在的命令行输入:

  public class Print {
public static void main(String [] args)throws异常{
//我知道这部分工作
JSAP jsap = new JSAP();
FlaggedOption opt3 = new FlaggedOption(cllmap)
.setRequired(true)
.setShortFlag('c')
.setLongFlag(call map);
opt3.setHelp(流感,地图文件在哪里?完整路径);
jsap.registerParameter(opt3);
String cllmp = config.getString(map);

我没有尝试引用我所做的任何新课程,因为我还没有想到怎么做正确呢
然后,我尝试将文件的位置发送到另一个类,以便其他类可以读取文件并将其解析为表对象。

  public class Reader extends Print {
String inpt;

public class FReader {
//读取
//抛出异常错误
FileInputStream fstream = new FileInputStream(cllmp);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while((inpt = br.readLine())!= null){
if(!inpt.equals(Calls)){
...

那么为什么我的类FReader会抛出异常以及如何让它停止?我不能说 public class FReader throws Exception {因为我然后得到错误:'{'expected 之后放置Fingerprint2
我在这里做错什么?任何建议?

解决方案

代码必须放在方法中,而不是直接在类体中。该类被称为 Reader ,所以它应该可能有一个 read()方法,这个方法应该读取,抛出一个IOException,如果它不能这样做:

  public class Reader扩展FingerPrint2 {
public String read()抛出IOException {
//输入您的代码
}
}


I am trying to refactor a large program in Netbeans and I am a bit lost. I haven't ever been very modular but am trying to correct that now, and for the future by actually learning how to do this. Unfortunately I am having trouble translating some of the tutorials to my beast of a program. SO I was hoping someone here could help. Currently I am trying to break off a chunk of code that takes a file of a specific format and makes a table. I know that I need to make a class and use it to create a table object but I am unsure how. I have a main that gets command line input for where a file is located:

public class Print {
  public static void main(String[] args) throws Exception {
            // I know this part works
     JSAP jsap = new JSAP();
     FlaggedOption opt3 = new FlaggedOption("cllmap")
            .setRequired(true)
            .setShortFlag('c')
            .setLongFlag("call map");
     opt3.setHelp("Where is the flu, map file? Full path");
     jsap.registerParameter(opt3);
     String cllmp = config.getString("map");

I haven't tried to reference any of the new classes I made yet because I have not figured out how to do it correctly yet. Then I try to send the location of the file to another class so that the other class can read in the file and parse it into a table object.

public class Reader extends Print {
 String inpt;

public class FReader {
    //reading in
 //throws exception error
    FileInputStream fstream = new FileInputStream(cllmp);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    while ((inpt  = br.readLine()) != null) {
        if (!inpt.equals("Calls")) {
          ...

So why does my class FReader throw an exception and how to I make it stop? I can't say public class FReader throws Exception{ because I then get the error: '{' expected same with putting it after extends Fingerprint2. What am I doing wrong here? Any suggestions?

解决方案

Code must be put in methods, and not directly in the class body. The class is called Reader, so it should probably have a read() method, and this method should read, and throw an IOException if it can't do it:

public class Reader extends FingerPrint2 {
    public String read() throws IOException {
        // PUT YOUR CODE HERE
    }
}

这篇关于类抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆