0

I got one doubt regarding exception handling. We have checked and unchecked exceptions. Why they have given like that? irrespective checked or unchecked we have to handle the exception. Then i am unable to draw a line why that is given like that.

For Example if we have ArithmeticException, It is not a checked exception. Then also we have to handle it when it occurs. Then what's the point of having two different kinds of exceptions?

Please help me in this. Thank you.

Praveen kumar
  • 90
  • 2
  • 7
  • 1
    "*irrespective checked or unchecked we have to handle the exception*" - ["*... the Java programming language does not require methods to catch or to specify unchecked exceptions...*" (`docs.oracle.com`)](https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html). – Turing85 Oct 26 '22 at 16:26
  • 1
    There's a large set of people who believe that checked exceptions in general are a failed experiment. It's impossible for anyone to answer your question for every single exception that exists in world because no one knows exactly what the original authors were thinking. I would expect that, as attitudes changed over time, an exception type that was added more recently is more likely to be unchecked, regardless of what it represents. – Michael Oct 26 '22 at 16:27
  • I am clearly not understanding, it closed my question as duplicate, Where as I didn't find same question any where. The other questions are related definitions of checked and unchecked. But what my doubt is why it is like that, even though we have to handle any kind of exceptions. – Praveen kumar Oct 26 '22 at 16:45
  • @Turing85 As per my understanding. If we throw Runtime or checked, who ever the caller he has to handle that exception right? Otherwise his code will break. Then why do we have two exceptions? Correct me if i am wrong or missing some thing. Thank you :) – Praveen kumar Oct 26 '22 at 16:52
  • Please read the link I provided in my first comment. Unchecked exceptions may not be handled. In such a case, they casecade up until they are caught or reach the entrypoint, where they lead to program termination. If we had to handle all exceptions, we would have, for example, to guard each array access with a `try`-`catch` block to catch `ArrayIndexOutOfBoundsException`s. – Turing85 Oct 26 '22 at 17:04
  • @Praveenkumar: Code is allowed to break. It's often a _good_ thing when it breaks, because it tells us that there's a bug in the code that needs to be fixed -- that usually isn't the exception itself. It is usually appropriate not to catch runtime exceptions and to allow them to break the program. – Louis Wasserman Oct 26 '22 at 17:12
  • Yeah i have gone through the link, and i understood what do you mean by code it allowed to break. Let me explain my doubt with idea i have with use case. For example one request came to controller, we autowired some service and called a method of that class, in that service class method we got exception(checked/unchecked) we didn't handle there, we let it propagate, so now exception came to controller from where this method is called. Then we will handle that exception and show some response to customer on browser right?, or we will let it cascade to browser and show stack trace on browser? – Praveen kumar Oct 27 '22 at 01:58
  • From the things i came across we show a decent message to end user. So here my point is, we will definitely handle that exception at some point in our program. Not to show stack traces on browser. Then why checked and unchecked? they said we are not supposed to catch runtime, but i have seen some other place we will catch by placing catch(Exception e) block. So please clarify my doubts. Thank you – Praveen kumar Oct 27 '22 at 02:07
  • Who said that we shouldn't catch `RuntimeException`s? We shouldn't catch `Error`s. And as to spring mapping the exception: [Spring has the concept of exception handlers (`baeldung.com`)](https://www.baeldung.com/exception-handling-for-rest-with-spring). Using Solution 3 allows defining a global "catch-all" handler that can catch everything that wasn't caught anywhere else and map it to a unified response. – Turing85 Oct 28 '22 at 21:27
  • @Turing85 Sorry its my bad. From starting on wards my concern is, at the end we have to catch any type of exception right? Whether by using spring or we did it by our selves. Then why do we have checked and unchecked? can you please clarify this. – Praveen kumar Oct 29 '22 at 07:55
  • 1
    Have you read the very first link I provided? The title is "*Unchecked Exceptions — **The Controversy***"- This *is* a controversial topic. Some people say "*use only checked exceptions*", some people say "*use only unchecked exceptions*", most people use a mixture. At this point, the question is primarily opinion-based. – Turing85 Oct 29 '22 at 08:31
  • @Turing85 Yes I have read the link you shared. But it didn't clarify my concern. like we have to handle any way, then why two types questions were there in my mind. Thanks for the Clarifications, From our discussion, I tend to agree it is opinion based. Thank you. :) – Praveen kumar Oct 29 '22 at 08:50

0 Answers0