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

JavaExceptionException HandlingTry Catch

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

Answer is No..It is 100% fine.. You might have to use lot of these in JDBC and IO, because they have lot of exceptions to be handled, one inside another...

Solution 4 - Java

Here is alternate solution if you don't want to use nested try and catch, You can also do it like this:

 boolean flag = false;
 void test();
 if(flag)
  {
   test2();
  }
  

Test Method goes here:

private void test(){
   try {
        Uri uri = Uri.parse("some url");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
   }catch (ActivityNotFoundException anfe){
        System.out.println(anfe);
        flag =true;
     }
 }

Now put the rest code in 2nd Method:

public void test2(){
  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                     
        }


  

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeekedOutView Question on Stackoverflow
Solution 1 - JavaT.J. CrowderView Answer on Stackoverflow
Solution 2 - JavaTomasz NurkiewiczView Answer on Stackoverflow
Solution 3 - JavaPeakGenView Answer on Stackoverflow
Solution 4 - JavaNoviceView Answer on Stackoverflow