File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes help on catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help on catch block" Watch "help on catch block" New topic

help on catch block

Hema lata
Ranch Hand

Joined: Jan 24, 2005
Posts: 34

In catch block can we assign values to string.

I have code like this

String result=""

//do some thing
catch(Exception e)
//if exception
result="failure"; >>>> can assigning operations like this be done in catch block??;!
return result;

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Have you tried it?

[Asking smart questions] [About Bear] [Books by Bear]
Hema lata
Ranch Hand

Joined: Jan 24, 2005
Posts: 34
thanks bear for the response!!
I have not tried that. Before trying wanted to know
is it valid to assign values to string in catch block??

please help me on this.

Sri Ram
Ranch Hand

Joined: Oct 03, 2005
Posts: 118
Thats y Catch Block is defined. U acn do what ever Operations u want when an Exception is thrown.
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

You can throw exceptions inside catch blocks, even the exception that was caught! (of course the catch block won't catch itself, you'd need another catch block for that)

It's useful in recovery actually, goes something like this...

<program throws an exception due to an error>
Mr. Catchblock: Ack! I've caught a new problem, must repair it...
Mr. Catchblock: Hmm, normally I can repair it, but this is way too complicated even for me.
Mr. Catchblock: Instead of exiting quietly I'm going to forcifully re-throw the exception I caught, hopefully someone else can deal with it.

[OCA 8 Book] [Blog]
Hema lata
Ranch Hand

Joined: Jan 24, 2005
Posts: 34
thank you sriram and scott.

your inputs helped me to complete certain logic..!

thanks again
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
In my experience, unless you're just changing/rethrowing the exception, a catch block almost always assigns string values to display and/or log error messages.
I agree. Here's the link:
subject: help on catch block
It's not a secret anymore!