C # MSVS the fastest way to remove try-catch blocks?

I took an incomplete project and, to my complete disbelief, each individual function is wrapped with try-catch phrases in the same format:

try { // work work. } catch(Exception ex) { MessageBox.Show(ex.Message, ...); } 

When I search for SO to quickly remove all of these try-catch blocks, I find that people are actually looking for a method to automatically transfer their functions with try-catch! hmm ... is that good programming practice at all? Is there a way to remove all blocks instead to make debugging easier and allows me to really allow exceptions?

+6
try-catch
source share
4 answers

You can change this parameter here:

 Debug -> Exceptions -> CLR Exceptions -> Check the "Thrown" checkbox. 

This causes the compiler to crash whenever an exception is thrown before checking catch blocks.

+7
source share

This is a terrible programming practice. I once saw this as an error that interfered with someone's database.

It is my firm belief that you would rather let your program die a deadly death than mindlessly continue in an unknown state.

I would find and replace with MessageBox.Show(ex with throw //MessageBox.Show(ex and replace them all. You will have to manually find those that really should be there and return them.

+2
source share

The search in Visual Studio Regex is quite powerful, but it’s a little difficult to use, here is what you might find useful when searching for your code above: (Note that in the search dialog box under “Options” select “Use: Regular Expressions”)

Find your bad catches:

.

catch * \ n +: {b +. [.: b \ n] MessageBox [.: b \ n] *}

If you want to replace with a throw:

catch \ n {\ nthrow; \ n}

+2
source share

I found a solution for VB.NET.

Replace this:

\s(?<!End )Try((.|\r\n)+?)Catch(.|\r\n)+?(Finally((.|\r\n)+?)End Try|End Try)

... with this:

$1$5

It will delete the entire try / catch block, leaving only what was in the try and finally block. However, it does not work with nested attempts / catches, so you need to replace the nested blocks first and then block the latter.

0
source share

All Articles