File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why we create our own Exception class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why we create our own Exception class ?" Watch "Why we create our own Exception class ?" New topic
Author

Why we create our own Exception class ?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 682

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





The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Oct 13, 2005
Posts: 36453
    
  15
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
Bartender

Joined: Oct 14, 2005
Posts: 18115
    
    8

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

Joined: Jul 03, 2011
Posts: 682

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 ???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why we create our own Exception class ?
 
Similar Threads
Exception handling question
understanding try-catch block?
how to print from 1 to 9 in a label ?
Concept on coding for Exception Handling
Exception handling throw and throws