• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

wirte my own Exception throw

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic