Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of try catch?

I'm bored with surrounding code with try catch like this..

try
{
    //some boring stuff
}
catch(Exception ex)
{
    //something even more boring stuff
}

I would like something like

SurroundWithTryCatch(MyMethod)

I know I can accomplish this behaviour by creating a delegate with the exact signature of the function, but creating a delegate for all the methods in my application it's just not an option.

I can also do this by injecting IL code but this is expensive in terms of performance as it creates an wrapper assembly around mine.

Any other valid ideeas?

like image 341
Razvan Dimescu Avatar asked Nov 11 '08 06:11

Razvan Dimescu


People also ask

How do I get rid of try catch?

Procedure. In the implementation part, select the TRY statement of the TRY CATCH statement you want to remove. Get the quick assist proposals by choosing Quick Fix from the context menu (shortcut Ctrl 1 ) or by using the Quick Assist view. Apply Remove Try Catch.

How do you deal with a try catch block?

Place any code statements that might raise or throw an exception in a try block, and place statements used to handle the exception or exceptions in one or more catch blocks below the try block. Each catch block includes the exception type and can contain additional statements needed to handle that exception type.

How do you remove try and catch in Java?

get(name); if(c == null)throw new IllegalArgumentException("Counter "+name+" not exist"); return c; } public void put(String name){ if(name==null)throw new IllegalArgumentException("null can`t be as name"); if(counters. get(name)!=

How do I stop multiple try catch blocks?

To avoid writing multiple try catch async await in a function, a better option is to create a function to wrap each try catch. The first result of the promise returns an array where the first element is the data and the second element is an error. And if there's an error, then the data is null and the error is defined.


2 Answers

Firstly, it sounds like you may be using try/catch too often - particularly if you're catching Exception. try/catch blocks should be relatively rare; unless you can really "handle" the exception, you should just let it bubble up to the next layer of the stack.

Now, assuming you really do want all of these try/catch blocks, why is it not an option to create a delegate? With anonymous methods and lambda expressions, as well as the Func/Action delegates in the System namespace, there's basically very little work to do. You write:

public void SurroundWithTryCatch(Action action)
{
    try
    {
        action();
    }
    catch(Exception ex)
    {
        //something even more boring stuff
    }    
}

and then your SurroundWithTryCatch(MyMethod) will work fine, if it takes no paramaters.

Alternatively, if you don't want to call a different method, just write:

public void MyMethod()
{
    SurroundWithTryCatch(() => 
    {
        // Logic here
    });
}

If you need to return from the method, you can do:

public int MyMethod()
{
    return SurroundWithTryCatch(() => 
    {
        // Logic here
        return 5;
    });
}

with a generic overload of SurroundWithTryCatch like this:

public T SurroundWithTryCatch<T>(Func<T> func)
{    
    try
    {
        return func();
    }
    catch(Exception ex)
    {
        //something even more boring stuff
    }    
}

Most of this would be fine in C# 2 as well, but type inference won't help you quite as much and you'll have to use anonymous methods instead of lambda expressions.

To go back to the start though: try to use try/catch less often. (try/finally should be much more frequent, although usually written as a using statement.)

like image 148
Jon Skeet Avatar answered Oct 13 '22 14:10

Jon Skeet


Aspect Oriented Programming could also help you, but this may require that you add libraries to your project, postsharp would help you in this case. See this link http://doc.postsharp.org/1.0/index.html#http://doc.postsharp.org/1.0/UserGuide/Laos/AspectKinds/OnExceptionAspect.html#idHelpTOCNode650988000

like image 26
Pablo Retyk Avatar answered Oct 13 '22 14:10

Pablo Retyk