• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

wirte my own Exception throw

 
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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:

 
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.
 
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.
 
Now I am super curious what sports would be like if we allowed drugs and tiny ads.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic