【问题标题】:Exceptions and errors being printed in wrong order以错误的顺序打印异常和错误
【发布时间】:2016-03-18 16:45:39
【问题描述】:

程序用于将程序参数解析为几何形状。

  • 圆 (C):1 arg [半径]
  • 五角大楼 (P):1 arg [边]
  • Square, Rhombus, Rect (Q): 5 args [a,b,c,d,angleA](程序选择哪个 一个基于 args 生成)

我的问题是控制台中打印异常/错误的顺序。

public static void main(String[] args)
{
    if (args.length > 0)
    {
        int readerIndex = 0; // currently read argument

        for (char symbol : args[readerIndex++].toCharArray())
        {
            ShapeType type = null; // Enum for C, P or Q.
            int toRead = 0; // How many args should we read.
            int read = 0; // How many we alredy read.

            try
            {
                type = ShapeType.bySymbol(symbol); // C, P or Q.
                toRead = type.getDataFormatSize(); // number of args to read.

                double[] vars = new double[toRead];
                // converting
                for (int i = 0; i < vars.length; ++i)
                {
                    vars[i] = Double.valueOf(args[readerIndex + i]);
                    ++read;
                }
                // This will create and print proper shape for given data.
                System.out.println(ShapeFactory.generateShape(type, vars).toString());
            }
            catch (ShapeSymbolException e) // thrown if letter is not a shape symbol.
            {
                e.printStackTrace();
            }
            catch (ArrayIndexOutOfBoundsException e) // when we are out of args.
            {
                System.err.println("Too few arguments to generate " + type.name() + "!");
            }
            catch (NumberFormatException e) // When we can't convert to double.
            {
                System.err.println("Could not convert String to double while generating " + type.name() + ". Offender: " + args[readerIndex + read] + " | Dumping arguments: " + Arrays.toString(Arrays.copyOfRange(args, readerIndex, readerIndex + toRead)));
            }
            catch (ShapeGenException e) // When data passed to ShapeFactory cannot generate proper shape from it.
            {
                e.printStackTrace();
            }
            finally
            {
                readerIndex += toRead; // In any case (exception or not) - we will be skipping args that were supposed to be parsed to shape.
            }
        }
    }
}

现在是数据输入:

  • C 10
CIRCLE (C) | Radius = 10.0
  • CQP 3 2 4 2 4 90 50
CIRCLE (C) | Radius = 3.0
QUADRILATERAL (Q) | RECTANGLE | Side A = 2.0 | Side B = 4.0
PENTAGON (P) | Side = 50.0
  • CQPPQ 3 2 4 2 4 90 50 w2 2 2 2 2 90
CIRCLE (C) | Radius = 3.0
QUADRILATERAL (Q) | RECTANGLE | Side A = 2.0 | Side B = 4.0
PENTAGON (P) | Side = 50.0
Could not convert String to double while generating PENTAGON. Offender: w2 | Dumping arguments: [w2]
QUADRILATERAL (Q) | SQUARE | Side = 2.0

“Could not conver...”部分随机打印在控制台中。它通常是它应该在的位置(按顺序排列第 4 个),但大约有 1/3 的时间它会出现在其他印刷品之一之前或之后。

我可以理解它稍后打印,但更早?我什么都想不出来。

为什么会这样?

【问题讨论】:

    标签: java exception try-catch


    【解决方案1】:

    System.errSystem.out 是两个独立的流 - 仅在单个流中保证顺序。由于两个流都以未定义的方式合并到您的控制台中,因此打印到两个流的文本出现的顺序可以改变。如果您将所有内容打印到System.out,您将获得有序的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多