• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why we create our own Exception class ?

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79952
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Don't listen to Steve. Just read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic