Win a copy of Head First Android this week in the Android forum!
  • 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 Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

problem with user defined exception

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created my own exception class and i m trying to throw the exception explicitly. But when i m compiling, its giving me the following error : Incompatible types error.

This is my code:


Can anybody tell me what's going wrong..??

Thanks in advance


 
Marshal
Posts: 74371
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which line is it on? And does throw new Exception().setErrorMessage("xxx") actually add up?
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to answer your question in two ways -- 1: to explain why your code doesn't compile, 2: to explain the proper way to write this method.

1: So as for the first part -- why your code doesn't compile:


When you use the "throw" clause, the method you call needs to return an Exception object

"throw new MyException()" in fact returns an Exception object, but you then chain that method with a call to setErrorMessage() which doesn't return anything, and so you get an incompatible Type error.

There are 2 ways you can fix this.
-- have setErrorMessage() return the MyException object.
-- construct and modify the Exception in one line, and throw the exception in the next line.

2- That being said, this isn't the "proper" way to use and define your own exception, a better way would be to define a constructor that takes the error message as an argument:


and then call the exception like this:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To improve it even further:

You do not need a member variable to store the message in your own exception class, because the superclass Exception already has such a member variable. Write your own exception class by implementing four constructors, like this:

Those four constructors correspond to the constructors of the base class Exception.

And then use it like this:

Use the standard getMessage() method from class Exception to see the message:

 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
much better -- nice Jasper.
 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot guys.....!!!
the information given by you was very helpful....
 
You showed up just in time for the waffles! And this tiny ad:
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
reply
    Bookmark Topic Watch Topic
  • New Topic