• Post Reply Bookmark Topic Watch Topic
  • New Topic

Create an exception  RSS feed

 
Pan Niko
Ranch Hand
Posts: 86
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Never created an exception class before, so im finding it lil difficult. I have a Message class that takes a String text. Asked to me to create MessageTooBigException when text message is longer than 50 char.
So until now i have:








As you can see my exception class still empty. How i should move on now?
thank you
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you know all about Exceptions already, particularly the difference between checked and unchecked Exceptions. It would appear you might want an unchecked Exception, possibly extending IllegalArgumentException or one of the IndexOutOfBoundsException s.
Usually, when you create an Exception, the difficult part is deciding which other Exception class to inherit from. Then creating the class is the easy part. In 99% of cases, all you have to do is find the superclass and create some constructors. You want four constructors, matching those in the java.lang.Exception class. You simply create those constructors with the super() instruction, like thisIf you get compiler errors about "can't find superclass constructor" it might be that your superclass only has two constructors. In that case you have two options
  • 1: Remove the constructors taking Throwable as a parameter.
  • 2: Use the initCause() method.
  •  
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    you should start by calling super(message) inside the constructor. When I create a new exception sub class that doesn't need any methods or fields of its own, all I do is "copy" all the super class' constructors:
    And that's it.

    Additionally you could add fields that indicate the maximum size and the actual size, which you both pass to the constructor. You can even use those to create a custom message:
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is a very unusual thread: I beat Rob by 1½ minutes.
     
    Pan Niko
    Ranch Hand
    Posts: 86
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for quick response, the only point i didnt get is how you check if actual message is larger than the limit. And where you checking this?
     
    Matthew Brown
    Bartender
    Posts: 4568
    9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You do a check wherever the message is being set to an invalid value. Probably in the Message constructor in this case. Then you just create the exception and throw it:
     
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Matthew Brown wrote:You do a check wherever the message is being set to an invalid value. Probably in the Message constructor in this case. Then you just create the exception and throw it:

    Which is easy with my second piece of code. The actual size is the length() of the String, the maximum size is the 50 you specified. So the throwing is then easy:
    The "whateverConditionYouWantToPutHere" should be easy enough to figure out. Hint: it includes the 50, message.length() and some comparison operator.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!