1

I've some piece of code related to Exception Handing for both c# and java.

As I've read in C# that finally block will not be executed when there's a StackOverflowException occurs since there's no room on the stack to even execute any more code and It will also not be called when there's an ExecutionEngineException occurs, which may arise from a call to Environment.FailFast().

In my below code(C#) I intentionally generate StackOverflowException :

 class overflow1

    {

      public  void disp()

        {
            try
            {
                Console.WriteLine("disp");
                disp();
            }

            catch(Exception e) {
                Console.WriteLine(e);
                Console.WriteLine("catch");
            }

            finally {
                Console.WriteLine("finalyyy");
            }
        }
    }

    class ExceptionHandling
    {
        static void Main() {

                overflow1 s = new overflow1();

                s.disp();

            Console.Read();
        }
    }

Output:

disp

disp

disp

.

.

.

Process is terminated due to StackOverFlowException

As I stated if StackOverflowException occurs since there's no room on the stack to even execute any more code of finally block. Even though catch block's methods also not called perhaps due to the same reason( no more space left into stack).

Now I've tried this above same code in java :

public class ExceptionHandlingTest {

  static  int count1 = 0 ,count2=0;
    static void disp()
    {

        try{

            System.out.println("disp"+(++count1));
            disp();

        }catch(StackOverflowError e){

             System.out.println(e);
             System.out.println("catch");
             System.exit(0);

        }finally{

         System.out.println("handle"+(--count1));

        }
    }

    public static void main(String... args)

    {
        disp();
    }
}

Output:

disp1

disp2

.

.

.

disp2456

handle2455

handle2454

.

.

Exception in thread "main" java.lang.StackOverflowError

.

.

handle2

handle1

handle0

Edit: Indeed my question is that behavior of code should be work same in both the languages because if all stack space filled then how in java JVM found more space to call finally block's methods ?

Thanks.

Community
  • 1
  • 1
Vikas Verma
  • 3,626
  • 6
  • 27
  • 40
  • Negative voters please mention reason in comments. – Vikas Verma Sep 23 '14 at 14:45
  • 1
    While it looks like you've spent good amount of effort on the question it likely will benefit from 2-3 lines TL;DR; section with clear single question. – Alexei Levenkov Sep 23 '14 at 14:47
  • 1
    Your question isn't entirely clear, what **problem** are you attempting to solve? Or are you inquiring on the behavior of C# VS Java Exception handling? *Could you outline your intent more clearly?* – Greg Sep 23 '14 at 14:47
  • Actually I think behavior of code should be same in both c# and java because StackOverFlow should behave same in both the languages and if not than how exception handling behave differently in both languages ? – Vikas Verma Sep 23 '14 at 14:50
  • @VikasVerma **Not the point, your question isn't clear.** – Greg Sep 23 '14 at 14:51
  • I downvoted because after I had read at least 1 page worth of "question" I still didn't see an actual question – Sam I am says Reinstate Monica Sep 23 '14 at 14:52
  • 1
    Are you aware that your Java `catch` will only catch **`Exception`s**, not **`Error`s** like `StackOverflowError`? – bcsb1001 Sep 23 '14 at 14:52
  • I am mainly talking about behavior of code should be work same in both the languages – Vikas Verma Sep 23 '14 at 14:55
  • @bcsb1001 that isn't necessarily true. You can write `catch(StackOverflowError e)` to catch it explicitly. – ccampo Sep 23 '14 at 14:56
  • @ccampo I know, as I stated in my answer. And how does that make my comment false? You must have misunderstood, I was talking about how the catch block will currently not catch `StackOverflowError`, as the OP seems to expect. – bcsb1001 Sep 23 '14 at 14:57
  • @bcsb1001 yep. understood now. upvoted the answer. – ccampo Sep 23 '14 at 14:58
  • Ok if I use catch(StackOverflowError e) behavior still same. – Vikas Verma Sep 23 '14 at 15:01
  • Its my bad that I use term StackoverFlowException sorry for that i'll change this in my question – Vikas Verma Sep 23 '14 at 15:02

1 Answers1

5

Your problem is in the Java catch block. Exception is not the superclass for all possible errors; Throwable is. StackOverflowError extends Error, not Exception, so isn't caught. Catching preferably StackOverflowError itself, but also VirtualMachineError, Error or Throwable will work.

bcsb1001
  • 2,834
  • 3
  • 24
  • 35
  • I really missed it's actually an Error at first sight .Still Can you point the difference in the outputs of both the languages ? – joey rohan Sep 23 '14 at 14:58
  • @joeyrohan I know nothing about C#, so nothing I can really say but it's a difference in how stack works internally. – bcsb1001 Sep 23 '14 at 15:06
  • 1
    @bcsb1001 sorry I totally changed my question but my query is related to internal working of stack but I appreciate your answer. – Vikas Verma Sep 23 '14 at 15:27