• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



OutPut
Print : exceptionHandlers.HighLevelException: exceptionHandlers.MidLevelException: exceptionHandlers.LowLevelException

Here the cause or the message is not printed.

How can i pass that, could some one help me out.

for example : i need an out put like

Exception : java.lang.ClassNotFoundException: sun.jdbc.odbc.jdbcodbcDriver
Where,

java.lang.ClassNotFoundException - is the exception happened
sun.jdbc.odbc.jdbcodbcDriver - is the corresponding message

In my example am not able to give out the message only the exception is getting printed.

i learnt toString method should be overridden.

But how do i pass the message from the mid level exception.

Please help me out !

Am for the past 2 days.

i will be highly thank full to you.


:roll: :roll:
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by seetharaman venkatasamy:
Try this



Hey,

you directly displayed the exception and message ;

return "java.lang.OwnException:you should not do this";

Let me explain this clear;

In java when an sqlexception happens, it gets the message from the DB and displays the same.

" When an exception like : Table or view does not exist, it says

java.sql.SQLException : Table or view does not exist

When some other sql exception happens like : inserted value too large for column

java.sql.SQLException : inserted value too large for column

The message changes, not the exception.

I need some thing like that.

Do java Exception - framework does it like this ?

(Exception and message within the toString() method)

return "java.lang.OwnException:you should not do this";

then i could follow the same.

please guide me to the right path , someone !

"
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i gave the code for the basic of building your own exception..

if you want your own messages when the SQLException araise,then you need to build logic in your java file(use some condition)...
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by seetharaman venkatasamy:
i gave the code for the basic of building your own exception..

if you want your own messages when the SQLException araise,then you need to build logic in your java file(use some condition)...



Do i need to call a constructor of type(Throwable,String)

and assign the string inside the overridden toString() method.

Could some one guide me !
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I strongly advise against overriding toString. Instead, create constructors that take a message:

If you pass a message (e.g. new HighLevelException("This is my message")), it will print its class name followed by ": " and the message. That's already the default toString() implementation.
If you use the constructor that only takes a Throwable it will use that Throwable's message instead.

This is how I usually create my exception classes - all four constructors (if the superclass allows it), and I can just choose which ones to use.
[ August 07, 2008: Message edited by: Rob Prime ]
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
I strongly advise against overriding toString. Instead, create constructors that take a message:

If you pass a message (e.g. new HighLevelException("This is my message")), it will print its class name followed by ": " and the message. That's already the default toString() implementation.
If you use the constructor that only takes a Throwable it will use that Throwable's message instead.

This is how I usually create my exception classes - all four constructors (if the superclass allows it), and I can just choose which ones to use.

[ August 07, 2008: Message edited by: Rob Prime ]




Hi DUDE,

I did that, i have posted the code here,

Thanks for your advice,but did you sense me not to override toString Method.

Is it not legal ?




Fine ! chaining of the exception too worked !

Code Snippet !



output :

User Defined Exception : exceptionHandlers.HighLevelException: TestMsgExeption


Now i have modified it as per my needs , is that a best practice to have that ' String msg ' static and use it.

or it has to be private static final "One exception msg for every string"

or it has to be an instance variable and all methods are non-static(in my context its all static.)

Also,
I understood that Throwable is the Exception class

like in an sqlexception !

java.sql.SQLException relates to 'Throwable var' and
table or view does not exist will be the 'String msg'

?1.Am i right with my understanding > ?

Please clarify !

Also, why do you say it Throwable instead of saying ExceptionTypeClass.

thanks for the advice you have given me

Also, PLease clarify me in other question !
[ August 08, 2008: Message edited by: ram kumar ]
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ram kumar:
Thanks for your advice,but did you sense me not to override toString Method.

Is it not legal ?


It's perfectly legal, but also completely unnecessary. The default implementation already does what you want, if you just use the right constructors of the super class.

Now i have modified it as per my needs , is that a best practice to have that ' String msg ' static and use it.

or it has to be private static final "One exception msg for every string"

or it has to be an instance variable and all methods are non-static(in my context its all static.)


I usually just use String constants when creating my exceptions. That way, you can use the same exception type but with different error messages.

I understood that Throwable is the Exception class

like in an sqlexception !

java.sql.SQLException relates to 'Throwable var' and
table or view does not exist will be the 'String msg'

?1.Am i right with my understanding > ?

Please clarify !

Also, why do you say it Throwable instead of saying ExceptionTypeClass.


Throwable is the superclass of Exception, and the root of everything that you can throw (hence the name . It also has other subclasses like Error. Although it isn't wise to do so you could catch Errors this way and wrap them in your Exception, by passing it as a parameter to the Exception constructor.
 
reply
    Bookmark Topic Watch Topic
  • New Topic