简体   繁体   English

为什么这个程序会出现异常?

[英]Why exception occurs in this program?

I have this program and always get the exception in the try catch block as "Something Wrong" in the output.我有这个程序并且总是在 try catch 块中得到异常作为输出中的“出错”。 Why is it that the output skips to the catch?为什么输出跳到catch? Here's the code:这是代码:

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");
    }
  }
}

What is exactly causing the exception in the catch block?究竟是什么导致了 catch 块中的异常?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM