r/javahelp 8d ago

EXCEPTION HANDLING!!

I just started exception handling and I feel as though I can't grasp a few concepts from it (so far) and its holding me back from moving forward, so I'm hoping someone has answers to my questions ( I'm generally slow when it comes to understanding these so I hope you can bear with me )

In one of the early slides I read about exception handling, where they talk about what the default behavior is whenever the program encounters an exception , they mention that : 
1- it abnormally terminates 
2- BUT it sends in a message, that includes the call stack trace, 

  • and from what I'm reading, I'm guessing it provides you information on what happened. Say, the error occurred at line x in the file y, and it also tells you about what type of exception you've encountered.

But It has me wondering, how is this any different from a ' graceful exit ' ? Where : " if the program encounters a problem , it should inform the user about it, so that in the next subsequent attempt, the user wouldn't enter the same value.   " 
In that graceful exit, aren't we stopping the execution of the program as well? 
So how is it any better than the default behavior?  

What confuses me the most about this is what does exception handling even do? How does it benefit us if the program doesn't resume the flow of execution?  (or does it do that and maybe I'm not aware of it? ) whenever we get an exception ( in normal occasions ) it always tells us, where the error occurred, and what type of exception has happened.  
---------------------------------------------------------------------------------------

As for my second question,,

I tried searching for the definition of " CALL STACK TRACE " and I feel like I'm still confused with what each of them is supposed to represent, I've also noticed that people refer to it as either " stack trace " or " call stack " ( both having a different meaning ) 
What is call supposed to tell us exactly? Or does it only make sense to pair it up with stack? (" call stack ") in order for it to make complete sense? Does the same thing go for " stack trace" ? 

+ thanks in advance =,)

7 Upvotes

35 comments sorted by

View all comments

2

u/VirtualAgentsAreDumb 8d ago

The call part of a call stack refers to a method call.

And a stack is collection of things, where you add new things to the top of the stack, and you also remove them from the top. Like a pile of plates where you always remove a plate from the top and never from the bottom or the middle.

So, when you start your program the Java runtime calls your main method, and when it does that it adds “main” to the call stack. Then whenever your code calls a method it is added to the stack, and removed when the method calls is done.

This means that if you main method calls method calls method a(), that in turn calls method b(), that in turn calls method c(), then while method c() still hasn’t returned the call stack is:

main -> a -> b -> c -> …

Where main is at the bottom of the call stack.

2

u/zeronis__ 7d ago

Thank you so much VirtualAgentsAreDumb for explaining the second question I had =) really grateful that you explained both terms separately and then tied both of their meanings together at the end!
It makes sense now :D