简体   繁体   中英

Try..Catch scenario

I sometimes find myself in scenarios like below. To state the question as simply as possible

"I sometimes will create a block of code that Java makes me surround in a try/catch statement. I have no use of the catch, so I leave it empty. Why is this wrong?"

 boolean operationSucceeded = false;                 
 JSONObject response = null;               

 try{ //Java wants you to surround JSONObject parsing with try/catch, and they usually have you surround lots of other things in try/catches

   response = new JSONObject(responseFromServerAboveNotRelevantToThisCodeBlock)
   //do something with response
   operationSucceeded = true;                      
 } 
 catch (JSONException e) {                  
     //what would happen here??                    
   }        

So in this scenario, I have no use of the catch block that I can think of. I don't have any need to log anywhere, and there isn't a point of setting the status to false again since I'm already assuming it's false, and setting true if the operation works (last line of code).

So I just have an empty catch, which is always bad and code analyzers say is wrong, etc, etc. What should I be doing? Is my code and line of thinking all wrong and needs to be logically restructured so that something actionable actually happens in my catch?

Thanks so much.

In my mind, the point of a try-catch statement is to keep your program from crashing, freezing, and so on even if there is an error. You can do something like, "e.printstacktrace()" or you can just have it continue, but it's a good idea to make your code be indicative of what you wish for it to do.

Is a good practice to always treat possible errors, log is very useful to find errors more quickly.

I think that's better..

     boolean operationSucceeded = true;                 
 JSONObject response = null;               

 try{ //Java wants you to surround JSONObject parsing with try/catch, and they usually have you surround lots of other things in try/catches

   response = new JSONObject(responseFromServerAboveNotRelevantToThisCodeBlock)
   //do something with response                     
 } 
 catch (JSONException e) {                  
     operationSucceeded = false;   
    // Some log here

   }  

I agree that there's often nothing relevant that can/should be done when an exception is caught. Even logging something is sometimes undesirable.

But when involved in code reviews, I expect, at minimum, a comment stating that the exception is being intentionally ignored. If it's not an obvious case, I expect the comment to state why it's being intentionally ignored.

Try/Catch block is very useful to handle program getting terminated due to an exception. The code in your try block may throw an exception and the catch block can be implemented to continue the program execution in an event of exception. In your case, although it is unnecessary to set the flag to false again but, you can write some useful code to inform the user why the code is going to terminate. It's also useful to the developer to debug as you can find out what exception is thrown by writing e.printStackTrace().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM