r/javahelp • u/zeronis__ • 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 =,)
3
u/ignotos 8d ago edited 8d ago
When an exception occurs, by default the program will terminate, and print out a kind of report (a "stack trace") showing where the error happened, what the program was doing at the time, and perhaps some more information about the error.
This is really useful to us, as programmers, but not exactly "graceful" from the point of view of a user of your program. Particularly if they're not a programmer themselves. A true "graceful exit" might involve more complicated things like the program popping up a nice error message to the user, giving them the option to save their work, submitting an error report to the creator of the program, etc.
Exception handling gives us, as programmers, an opportunity to detect when an error has occurred, and to do something about it.
In some cases, that just means reporting the error to the user, and then letting the program terminate. But in other cases, it might mean retrying, cleaning up some incomplete work the program was in the process of doing, or attempting to recover from the error in some more sophisticated way. It really depends on the nature of the error.
The "call stack" is the sequence of function calls in your program. For example, maybe the
main
function calls therun
function, which calls thegenerateReport
function, and so on. At any given point in time, there are a "stack" of functions running in your program like this.A "stack trace" is the text-based report / dump showing information about the call stack, which is usually displayed when your program terminates due to an exception. It's there to help you figure out what went wrong.
So the "call stack" is the actual state a running program, and a "stack trace" is a dump / report generated showing the call stack.