Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - is it bad practice to do a try/catch inside a try/catch?

I have some code that I want to execute if an exception happens. But that code can also generate an exception. But I have never seen people do a try/catch inside another try/catch.

Is what I am doing poor practice and maybe there is a better way of doing this:

 Uri uri = Uri.parse("some url");  Intent intent = new Intent(Intent.ACTION_VIEW, uri);   try   {      startActivity(intent);  }   catch (ActivityNotFoundException anfe)   {      // Make some alert to me       // Now try to redirect them to the web version:      Uri weburi = Uri.parse("some url");      try      {          Intent webintent = new Intent(Intent.ACTION_VIEW, weburi);          startActivity(webintent);      }      catch ( Exception e )      {          // Make some alert to me                              }  } 

It seems kind of awkward. Is there something that might be wrong with it?

like image 910
GeekedOut Avatar asked May 20 '12 14:05

GeekedOut


People also ask

Can you have a try-catch within a try-catch Java?

Yes, we can declare a try-catch block within another try-catch block, this is called nested try-catch block.

Is it good practice to use nested try-catch?

I actually don't think there's anything inherently wrong about nested Try / Catch blocks, except that they can be difficult to navigate and are likely a sign that you could do some refactoring (the inner Try / Catch into its own method, for example).

Is try-catch bad practice in Java?

There's nothing wrong with having try-catch in a method, and there's nothing wrong with having try-catch inside another try-catch . The only thing that matters, is whose job it is to handle exceptions. Sometimes you have a method whose only job is attempt to perform a task, and not deal with unusual conditions.

Is it good practice to throw exception in catch block?

It's fine practice to throw in the catch block. It's questionable practice to do so ignoring the original exception.


2 Answers

It's fine, although if your exception handling logic is that complex, you might consider breaking it out into its own function.

like image 132
T.J. Crowder Avatar answered Sep 18 '22 10:09

T.J. Crowder


It is a bad practice to write code with so many levels of nesting, especially in try-catch - so I would say: avoid. On the other hand throwing an exception from catch block is unforgivable sin, so you should be very careful.

My advice - extract your catch logic into a method (so catch block is simple) and make sure this method will never throw anything:

Uri uri = Uri.parse("some url"); Intent intent = new Intent(Intent.ACTION_VIEW, uri);  try  {     startActivity(intent); }  catch (ActivityNotFoundException anfe)  {     // Make some alert to me      // Now try to redirect them to the web version:     Uri weburi = Uri.parse("some url");     Intent webintent = new Intent(Intent.ACTION_VIEW, weburi);     silentStartActivity(webintent) }   //...  private void silentStartActivity(Intent intent) {     try     {        startActivity(webintent);     }     catch ( Exception e )     {         // Make some alert to me                          } } 

Also it seems (I might be wrong) that you are using exceptions to control program flow. Consider standard return value if throwing ActivityNotFoundException is not an exceptional situation but it might happen under normal circumstances.

like image 35
Tomasz Nurkiewicz Avatar answered Sep 21 '22 10:09

Tomasz Nurkiewicz