Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why we create our own Exception class ?

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was trying , and actually i had make my own exception class , but i m not getting it , why we need to make our own exception class , i m asking this because we can use Exception class or any of its sub class , and with the help of try catch we can give whatever error we want to give to our user for a particular exceptions , because we know what will be the expected exception at this particular block of code

like consider the two different program handled with different Exception class
1. is handled with myown Exception class
2. other is handled with the predefined super class called Exception

NOTE : but user will get appropriate error msg on the console saying



 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is usually very easy to create your own Exception: you do it exactly the same way you create a subclass of anything else. You usually only need to create 4 overloaded constructors. The hardest part is usually choosing an inheritance hierarchy.

This sort of question appears here frequently.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If everything just throws Exception, then it makes it impossible for the calling code to be able to differentiate between different things that might have gone wrong. But it might want to treat different types of error in different ways. That's where using different exception types comes in really useful.
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seem to have misunderstood the original question.

It means you can create a NavinsException and use that as a hierarchy all of its own, for catch() purposes. It means all other Exceptions can be handled completely separately.
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Navin's question is essentially the same as "Why is there more than one Exception class?"

So why did the people who wrote the java.sql package decide they needed an SQLException? Couldn't they just throw Exception? And all those other hundreds of classes in the standard API which extend Exception? What's the point of having all of them?

Well, obviously it's because they can be used for a specific purpose. Just like any other class, if you need something for a specific purpose then you write it.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Navin's question is essentially the same as "Why is there more than one Exception class?"

So why did the people who wrote the java.sql package decide they needed an SQLException? Couldn't they just throw Exception? And all those other hundreds of classes in the standard API which extend Exception? What's the point of having all of them?

Well, obviously it's because they can be used for a specific purpose. Just like any other class, if you need something for a specific purpose then you write it.

well thanks thats what i thought ,and one more thing is that why we create constructor of our newlly created Exception clsss because we never through error saying that what we have wrote in a parameter
say like


and some where down we write something like
throw new MyException(" Unknow error ");


but we never display that error on the console or to our user , instead we will use try catch and in catch we will write something more specific related with the error or exception ,so now the question is why we write constructor and give parameters to those constructors ???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic