Multiple try-catch or one?

前端 未结 11 1728

Normally, I\'d do this:

try
{
    code

    code that might throw an anticipated exception you want to handle

    code

    code that might throw an anticip         


        
11条回答
  •  轮回少年
    2020-12-08 09:34

    There are time we want to show specific error to user.

    try{
       try{
          ... send message
       }
       catch(exception e){
        ...log error
        ...rethrow  send related error/show custom error
       }
       try{
           ...try to receive response
       }
       catch(exception e){
           ...show receive related error
       }
       //finally close the connection
       }finally{
            ...close connection
       }
    

提交回复
热议问题