为什么在这个程序中发生异常?

时间:2015-09-12 17:21:03

标签: java exception-handling

我有这个程序,并且总是在try catch块中得到异常,因为" Something Wrong"在输出中。为什么输出跳到了捕获?这是代码:

import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

class pgm {
  public static void main(String[] args) {
    StringBuffer sa = new StringBuffer();
    StringBuffer n = new StringBuffer();
    StringBuffer sb = new StringBuffer();

    try {
      FileReader fr = new FileReader("1.txt");
      int i;

      while ((i = fr.read()) != -1) {
        n.append((char) i);
      }
      fr.close();
    } catch (Exception e) {
    }

    String[] lines = String.valueOf(n).split("\n");

    int x = lines.length;
    int count = 1, m = 0;

    try {
      FileWriter fw1 = new FileWriter("1a.txt");
      FileWriter fw2 = new FileWriter("1b.txt");

      String linea = Files.readAllLines(Paths.get("1.txt")).get(1);
      sa.append(linea);

      for (int i = 2; count <= x; i++) {
        String lineb = Files.readAllLines(Paths.get("1.txt")).get(count);
        String ab = lineb;
        ab = ab.replace("\n", "");
        count++;
        String linec = Files.readAllLines(Paths.get("1.txt")).get(count);
        ab += linec;
        sb.append(ab);
        count++;
        m = 0;

        for (int j = 0; j < 5; j++) {
          String lined = Files.readAllLines(Paths.get("1.txt")).get(count);
          if (lined == "\n") {
            count++;
            m++;
          } else {
            break;
          }
        }

        for (int j = 0; j < m; j++) {
          sa.append("\n");
          sb.append("\n");
        }

        String linee = Files.readAllLines(Paths.get("1.txt")).get(count);
        sa.append(linee);
      }

      String sa1 = String.valueOf(sa);
      String sa2 = String.valueOf(sb);

      fw1.write(sa1);
      fw2.write(sa2);
      fw1.close();
      fw2.close();
    } catch (Exception e) {
      System.out.println("Something wrong");
    }
  }
}

究竟是什么导致了catch块中的异常?

1 个答案:

答案 0 :(得分:1)

是的,如果您不打算处理或重新抛出异常,则不应该抓住它们。