0

I have a big complex project where I started out to use try and catch like this :

try
{
}
catch (Exception ex)
{
   throw new ExceptionHandler("", ExceptionType.UnexceptedException, ExceptionSeverity.Error, null, "", ex);
}

Now I need to remove this because it have shown to be a bad pattern to follow. Its to much work to do it manually so the question is if there is any tool that could help me? In some cases there might be a finally and if so, then the try should not be removed.

Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
Banshee
  • 15,376
  • 38
  • 128
  • 219
  • Is it not an option to search and replace the line? – usselite May 24 '16 at 07:06
  • 1
    Write an application that does it for you? – Patrick May 24 '16 at 07:11
  • 1
    You can write regexp to find them for you I suppose (visual studio supports search with regexes for example). Then you can visually inspect each concrete case and remove by hand, or if you are brave enough - automatically remove all matches. – Evk May 24 '16 at 07:12
  • Its way to much to do this by hand(7 years with 6 devs and try/catch in every method). I just want to remove the above pattern, nothing else. Regex sounds good, the problem is that my knowledge is more or less none about regex. – Banshee May 24 '16 at 07:20
  • Well if you provide the exact pattern you want to remove (you don't want to remove _all_ try\catch blocks, right?) - we might try to help you with regex. – Evk May 24 '16 at 07:24
  • @Banshee And that's what [regexlib](http://regexlib.com/) is for. Also, there is a program called [Expresso](http://www.ultrapico.com/Expresso.htm) that is extremely good for making regular expressions. – Nahuel Ianni May 24 '16 at 07:25

3 Answers3

5

To be honest, I think that the best solution here is to do this by hand. Automated tools are great for the base case of things but chances are the somewhere in the code, someone has done something that is a little specialised for a specific use case or has some extra logging.

My advice is to search for all instances of this, but to replace/remove them one by one manually to ensure that everything still operates as expected.

David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • My thoughts exactly. Bad exception handling is a problem, but just removing it is inviting huge loads of trouble - this needs a programmer's hand. – Luaan May 24 '16 at 07:14
  • But in this case I just want to remove try{#ignore#}catch (exception ex){throw new ExceptionHandler("", ExceptionType.UnexceptedException, ExceptionSeverity.Error, null, "", ex);} and nothing else. If the above pattern is not matched then it should not be removed. Its way to much to do it manualy. Think 7 year dev with 6 devs and try/catche in every method. – Banshee May 24 '16 at 07:17
0

Try to write script on python with regular expression on each file in folder recursively. I think it can be a solution

  • lol, why specifically in python? "I think it can be"? Then this is not really a solution is it, if you are not sure? – Patrick May 24 '16 at 07:58
  • Sorry, will formulate in another way. Write script on any scripting language with regular expression on each file in folder recursively. –  May 24 '16 at 08:37
  • It still sounds like a suggestion or comment. You're not providing a real answer to the question other than "you need to fix it yourself somehow". – Patrick May 24 '16 at 10:53
0

Find/Replace is not so recommended, although you can do it and then fix the specific cases.

The most recommended way IMO is to use Roslyn to find and handle all specific cases.

There is alot of examples out there. See this and this for example.

Community
  • 1
  • 1
Dudi Keleti
  • 2,946
  • 18
  • 33