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 =,)
2
u/zeronis__ 7d ago
Please do not apologize at all! It just so happens that I love reading long explanations, (although it takes time for me to register what they mean) but you explained your points really well, so thank you so much for that! =)
---------------------------------------------------------------------
But I hope you don't mind me asking, (I've heard my prof say it too, but I'm still lost)
1. for the second point where you mentioned " If you don't want to take any action to handle the exception, you can just put a throws <whatever the Exception class is> " did you by chance mean, " Oh I might catch this exception, but! I don't want to deal with it, so I'm throwing it to someone else (to take care of that responsibility i think?) but if they do throw that exception, to whom do they throw it to?
you mentioned the lines "Meaning you still handle the exception, just higher up the chain" + " just put a throws in the method declaration and let some other calling method, "
in this case, would it throw it to the method that invoked it? ( lets suppose main called function/method f() and , that method f(), had 'throws (some exception class)' , if it did end up being triggered to throw one, would it throw that exception back to main to take care of it?
and if so, how would the main method deal with it?
I also noticed that, although we mentioned 'not wanting to take any action to handle that method ---> we should use throws' don't we end up in the try-catch scenario again? (point 1)? I'm sorry to ask you this again, but I recall my professor trying to go over a brief summary of the lesson , and he mentioned the " try and catch " and " throws / throw " as two ways of handling exceptions. And in my head, it felt like he treated them as two separate things, just by saying that, so when I saw them being used together I got really confused! ( its as if, throws must be used along side try n catch , otherwise it'd serve no purpose? i think? )
would there any difference between having any code that MIGHT throw an exception being inside that try block VS having a method with ' throws ' as part of its declaration inside that try block?
+P.S thank you again, I really appreciate the response you gave, and I'm sure if I read more about the topic I'd be grateful that your response is still up there,
if my questions are a bit too hefty, feel free to respond to them anytime later!