aspose file tools*
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146



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:


Discussion - the powerfull way to excellence!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Try this
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: May 22, 2008
Posts: 146
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 !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
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

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception Handling