Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wirte my own Exception throw

 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please provide some examples how to write Exception throw. I always use standard library.
As now, I like to learn how to write my own Exception throw.

Thank you very much
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15366
40
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what you mean by "writing my own Exception throw". Are you asking how to write your own Exception class?

That's easy: just create a class that extends Exception, or RuntimeException if you want it to be an unchecked exception. Since class Exception has 4 constructors, you might want to add the same 4 constructors to your own Exception class:
 
Kee Kee moon
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, if users enter a number that is less than 0. I like to use try and catch to handle exception.
It seems that I could not find a standard library for the exception handling.


Jesper de Jong wrote:I don't know what you mean by "writing my own Exception throw". Are you asking how to write your own Exception class?

That's easy: just create a class that extends Exception, or RuntimeException if you want it to be an unchecked exception. Since class Exception has 4 constructors, you might want to add the same 4 constructors to your own Exception class:
 
Ashutosh M Kulkarni
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kee Kee moon wrote:For example, if users enter a number that is less than 0. I like to use try and catch to handle exception.
It seems that I could not find a standard library for the exception handling.

Oh, it seems you have a very simple problem. If the requirement is like quoted above, then you could do with a simple if statement.
Although, it should be noted that this is a very generic example and it is NOT advised to use Exception class itself for throwing exceptions, a more specific Exception subclass would be more appropriate (the kind that Jasper suggested).
You'd best write your own exception class as demonstrated in the earlier post, that only caters to specific situations (in you case, less than zero).

Also, please notice that you must use the "throws" keyword to declare the exception while declaring the method, otherwise compiler will complain.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IllegalArgumentException would seem to be a good choice here. You could subclass it if you want to make the exception even more specific.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic