• 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

help on catch block

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

In catch block can we assign values to string.

I have code like this

String result=""
try{

//do some thing
result="success";
}
catch(Exception e)
{
//if exception
result="failure"; >>>> can assigning operations like this be done in catch block??;!
System.out.println("Exception"+e);
}
return result;

thanks
hema
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it?
 
Hema lata
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


thanks
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats y Catch Block is defined. U acn do what ever Operations u want when an Exception is thrown.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Hema lata
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you sriram and scott.

your inputs helped me to complete certain logic..!

thanks again
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic