• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Is checked exceptions a liability feature in Java unlike C# which has only unchecked exceptions

 
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C# has only the concept of checked exceptions. Java has both checked and unchecked exceptions. Are checked exceptions a liability in Java for the developers unlike C# which has only unchecked exceptions.

thanks
 
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Monica,

I am not familiar with C#. In fact, I am just getting acquainted with java. I am not sure what you mean by "liability" but when it comes to checked exceptions in Java (as opposed to unchecked exceptions), you are obliged to cover your back, i.e. you must either "handle or declare" checked exceptions.
 
Saloon Keeper
Posts: 10494
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:C# has only the concept of checked exceptions.


I think you mean, C# only has unchecked exceptions.

Like Brecht, I don't know what you mean by liability.

Personally I'm a big fan of checked exceptions, because the compiler reminds me I have to take care of them. Many others consider checked exceptions a "failed experiment", because lazy programmers don't care to handle them properly and just declare a throws clause on all their method signatures.
 
Monica Shiralkar
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

because the compiler reminds me I have to take care of them.

Thanks. Yes that is a reason.




lazy programmers don't care to handle them properly and just declare a throws clause on all their method signatures.



If the programmers keep declaring throws clause from the methods and catch it from the method which calls these methods , then what is wrong with this way?
 
Stephan van Hulst
Saloon Keeper
Posts: 10494
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because they don't catch the exception in the calling method. They just declare another throws clause in that method.

It's okay to let some exceptions bubble up the call stack until they reach the appropriate method to handle them, but many programmers just let exceptions bubble all the way up, or even worse, handle the exception too soon by just logging a message (or sometimes not even that!)
 
Brecht Geeraerts
Master Rancher
Posts: 172
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the cow. It is always a pleasure to receive one.  I will take good care of my cattle
 
Master Rancher
Posts: 3313
31
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It's okay to let some exceptions bubble up the call stack until they reach the appropriate method to handle them, but many programmers just let exceptions bubble all the way up, or even worse, handle the exception too soon by just logging a message (or sometimes not even that!)



This "even worse" situation is actually one of the reasons I dislike checked exceptions - they seem to encourage many other programmers to catch exceptions when they shouldn't.  We may denigrate these other programmers as "lazy" I suppose, but I would prefer to say "inexperienced".  Regardless, I've seen far too much badly-written exception handling code out there, and I would say that whenever people just aren't sure how to handle an exception, it probably is better to just let it bubble up, rather than catching it but not handling it effectively.  At least if it bubbles up, the exception will be easier to see clearly, and people won't be distracted trying to understand why subsequent code failed - they'll be much closer to the root cause.

----

I think it's worth noting that there have been quite a few languages and frameworks that came along after Java, often taking the most useful ideas from it - just as Java had built on some of the best ideas from programming languages before it.  Some of the more prominent successors to Java include C#, Groovy, Scala, and Kotlin.  As far as I know, none of these have chosen to incorporate checked exceptions.  It's not a language feature that others have chosen to emulate.  

Admittedly, I may be missing something here.  I'd love to hear about it if anyone knows of other languages that do use checked exceptions.
 
Monica Shiralkar
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks. Although it is different from the original question, I am thinking that why does Java not come up with a feature where you do not need to explicitly provide try catch and whatever coding you do that implicitly comes under a try catch.
 
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:. . . why does Java not come up with a feature where . . . whatever coding you do that implicitly comes under a try catch.

Use the “surround with try‑catch” feature on your IDE.
 
Stephan van Hulst
Saloon Keeper
Posts: 10494
224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would that even mean Monica? Would my exceptions be caught even if I don't want them to be caught? That's decidedly bad.
 
Sheriff
Posts: 13675
226
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:I am thinking that why does Java not come up with a feature where you do not need to explicitly provide try catch and whatever coding you do that implicitly comes under a try catch.


I agree with Stephan, this is not a good idea.

For better or worse, this is the exception handling mechanism the designers of Java decided to implement and we as developers must learn to live with it. There are pros and cons to the approach. From my experience, the cons are mostly related to developers not using the framework properly. This idea sounds a lot like it's coming from a place of confusion about how to properly handle exceptions. The answer is not to wish for a feature with dubious value but to learn better exception handling approaches that make exception handling and programming around exceptions less onerous.

I prefer the general approach used by Spring to handle checked vs unchecked: wrap low-level checked exceptions with unchecked exceptions and let the developers decide where they want to handle it. If nobody handles it, the exception just keeps getting propagated back up the application stack where it either stops the application or gets handled by a generic catch-all exception handler.
 
Mike Simmons
Master Rancher
Posts: 3313
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:I prefer the general approach used by Spring to handle checked vs unchecked: wrap low-level checked exceptions with unchecked exceptions and let the developers decide where they want to handle it. If nobody handles it, the exception just keeps getting propagated back up the application stack where it either stops the application or gets handled by a generic catch-all exception handler.



Which, in my opinion, is basically our way of working around the inconvenience of checked exceptions.  Previously I listed languages that don't use Java, but there are also frameworks and libraries in Java that convert checked to unchecked exceptions as much as possible - Spring and Hibernate are the biggest examples I know of.
 
Marshal
Posts: 7070
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Monica Shiralkar,

CowGratulations, your topic has been published in our CodeRanch Journal August Edition.

A copy of journal you could find here -> https://coderanch.com/wiki/715405/CodeRanch-Journal-August
Other than that, you, as everybody else supposed to get one emailed to you
 
Monica Shiralkar
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Monica Shiralkar
Ranch Hand
Posts: 1172
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would be an example of case where one would not want his exceptions to be caught?
 
Campbell Ritchie
Marshal
Posts: 65365
248
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you asking the right question? Surely you should ask this sort of thing about every occasion where an excception can be thrown:-

What am I going to do if this exception is thrown?

Sometimes the answer is,

Pass it on to the calling method.

Sometimes you cannot work out what to do about an exception at all. Sometimes, as often with null pointer exceptions, it is necessary to prevent them rather than handling them, and that might even require alterations to the code.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!