多个System.out.println()调用显示在一行上?

时间:2014-11-28 22:05:55

标签: java error-handling

这个程序给出了一些奇怪的输出。在多次执行此循环后,我看到一行输出,其中包含三个独立的.println()函数的内容。为什么会这样?

警告:此程序在无限循环中运行。

public class Looping
{
    static int i;
    public static void main(String agrs[])
    {
        try
        {
            i++;
            System.out.println("In try String agrs" +i +" ");
            main(i);
            System.out.println(i+"After main(i).**************************!!!!!!!~~~~~~~~~~~~~```````++++_(^&$^%&%$#%$^$#%^&Y%$^#%^%&@#%#$%");
        }
        catch(Error e)
        {

            System.out.println("Catch reached"+i);
            Looping q=new Looping();
            q.main(++i);
        }
    }

    public static void main(int k)
    {
        try
        {
            i++;
            System.out.println("In try int k -- " +i +" __ ");
            main(i);
            System.out.println(i+"After main(i).**************************!!!!!!!~~~~~~~~~~~~~```````++++_(^&$^%&%$#%$^$#%^&Y%$^#%^%&@#%#$%");
        }
        catch(Error e)
        {
            System.out.println("Catch of int k -- " + i + " Why there is no new line here???______");
            i++;
            System.out.println("i increasing done.. next executes main(++i)");
            main(++i);
        }
    }
}

这是输出(删除了一些中间线):

In try String agrs1 
In try int k -- 2 __ 
In try int k -- 3 __ 
In try int k -- 4 __ 
In try int k -- 5 __ 
In try int k -- 6 __ 
In try int k -- 7 __ 
In try int k -- 8 __ 
In try int k -- 9 __ 
...
...
...
...
..
...
In try int k -- 2889 __ 
In try int k -- 2890 __ 
In try int k -- 2891 __ 
Catch of int k -- 2891 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2894 __ Catch of int k -- 2894 Why there is no new line here???______Catch of int k -- 2894 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2897 __ 
In try int k -- 2898 __ Catch of int k -- 2898 Why there is no new line here???______Catch of int k -- 2898 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2901 __ 
In try int k -- 2902 __ Catch of int k -- 2902 Why there is no new line here???______Catch of int k -- 2902 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2905 __ Catch of int k -- 2905 Why there is no new line here???______
Catch of int k -- 2905 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2908 __ 
In try int k -- 2909 __ 
In try int k -- 2910 __ Catch of int k -- 2910 Why there is no new line here???______Catch of int k -- 2910 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2913 __ 
In try int k -- 2914 __ Catch of int k -- 2914 Why there is no new line here???______Catch of int k -- 2914 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2917 __ Catch of int k -- 2917 Why there is no new line here???______
Catch of int k -- 2917 Why there is no new line here???______
i increasing done.. next executes main(++i)
In try int k -- 2920 __ 
In try int k -- 2921 __ 
In try int k -- 2922 __ 
....
.....
..

1 个答案:

答案 0 :(得分:1)

println()库函数中抛出异常。

仪表后,我看到了:

java.lang.StackOverflowError

java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)
sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271)
sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
java.io.PrintStream.write(PrintStream.java:526)
java.io.PrintStream.print(PrintStream.java:669)
java.io.PrintStream.println(PrintStream.java:823)
Looping.main(Looping.java:41)

你不仅仅是无限循环,而是在无限的嵌套调用中。这会填满调用堆栈,您的程序和您进行的任何库调用都会使用它。当println()调用它的工作时,它正在进行调用。在某些时候,它会耗尽堆栈空间并抛出异常。这将使您脱离现有的catch子句,进入下一个更高级别的嵌套catch子句。