Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptions  RSS feed

 
ShivaKumar Singani
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java ,we have runtime and user defined exceptions.
for runtime ,the JVM will handle.
and the user defined are handled by the user by using try and all those.
my question is why we need user defined exceptions?
coz we can handle those by using some conditions also
then wats the need for user defined exceptions?

thanks
 
Shelemiah Castro
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi (I'm excited this is my first post!)

As an answer to your inquiry: We can opt to use, suggested to use appropriately, customized exceptions so we can take advantage of one of the many benefits of Object-Oriented Programming which is code-reusability. That
instead of re-writing similar code for handling faulty conditions, we call our custom-written exceptions, similar to instantiating objects of another class when we need them. No need to re-invent the wheel.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java there are checked and unchecked exceptions. It is a bit inaccurate to talk about "user defined exceptions", because you can create your own checked as well as unchecked exceptions. So, user defined exceptions are not necessarily checked exceptions.

The advantage of checked exceptions is that they provide safety: they force the programmer to think about error conditions that can happen, because you are required to handle the exception - either by adding a try-catch block, or adding a throws-clause to the method declaration.

On the other hand, there are people who think that checked exceptions are not needed. Checked exceptions are a feature that is unique to Java, no other programming language has the same concept.

For more details about how exceptions work in Java, see The Java Tutorial - Exceptions.

Critique on checked exceptions:

Bruce Eckel: Does Java need Checked Exceptions?

The Trouble with Checked Exceptions (about why C# does not have checked exceptions; Anders Hejlsberg, the designer of the C# language, explains what he thinks are issues with checked exceptions)

Avoiding checked exceptions

And Shelemiah, welcome to JavaRanch!
 
Shelemiah Castro
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Shelemiah, welcome to JavaRanch! [jumpingjoy]


Thanks Jesper.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!