• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can we create our own exception in java?If yes how can it possible?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. By extending java.lang.Exception.
 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I replied the same but in interviews i recived the reply no.....
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then be glad you were turned down, because they obviously didn't know a thing about programming themselves. It would only be a matter of time before you would have sent a story to The Daily WTF

But the above answer is not complete. You can extend Exception, as well as any subclass of Exception. That includes RuntimeException which will make your exception non-checked as well.
 
Jon Parise
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
usually I make a class that extends exception.

The thing that trips people up is that Exception's attributes like message are not accessible in the subclass.

So instead you need to simply overload the constructors of Exception.

Here is the code for an exception I wrote recently:


It's that simple to make a custom exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic