Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes wirte my own Exception throw Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "wirte my own Exception throw" Watch "wirte my own Exception throw" New topic
Author

wirte my own Exception throw

Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
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

Joined: Aug 16, 2005
Posts: 14074
    
  16

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147
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

Joined: Jun 07, 2010
Posts: 41

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.


SCJP 6, next stop - OCPJWCD!
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
IllegalArgumentException would seem to be a good choice here. You could subclass it if you want to make the exception even more specific.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wirte my own Exception throw
 
Similar Threads
Exception handling: throw custom exception while using apis
Overrding an exception
keep getting NullPointerException error
Doubt
passing control between classes