Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could i let Resharper or Intellisense know that a method allwayse throws exception?

I have an method that throws exception :

public void MyMethod()
{
    // do some logging
    throw new Exception("My Text");
}

I'm using this method in more than 100 places in my code, the problem is when i use this method in another method that must return a value Re-sharper dose not understands that there is no need for returning a value, for example :

public int AnotherMethod()
{
    // some stuff
    MyMethod(); // <= it always throws exception

    return 0; // i have to put this code, while it never executes
}

is there any way to tell Re-sharper that MyMethod is an exception just like this:

public int AnotherMethod()
{
    // some stuff
    throw new Exception(); // <= it always throws exception        
    //return 0; <= this line is no more needed
}
like image 501
Ehsan Zargar Ershadi Avatar asked Jan 04 '15 09:01

Ehsan Zargar Ershadi


People also ask

How do you know if a method throws an exception?

In order to test the exception thrown by any method in JUnit 4, you need to use @Test(expected=IllegalArgumentException. class) annotation. You can replace IllegalArgumentException. class with any other exception e.g. NullPointerException.

How do you call a method that throws an exception?

The throws keyword is used to declare which exceptions can be thrown from a method, while the throw keyword is used to explicitly throw an exception within a method or block of code. The throws keyword is used in a method signature and declares which exceptions can be thrown from a method.


2 Answers

There's two issues here.

First, you can give ReSharper hints to code analysis, and I'll show you how below.

The problem here, however, is not ReSharper, but the compiler. The compiler will not be persuaded by these hints. The method you showed:

public int AnotherMethod()
{
    // some stuff
    MyMethod(); // <= it always throws exception

    return 0; // i have to put this code, while it never executes
}

Will have to be written like this even if you persuade ReSharper to understand that MyMethod always throws an exception. This has nothing to do with ReSharper, this is solely the C# compiler.

In order to tell ReSharper that your method behaves in a certain way, you can tag it with an attribute. You need to either copy a source code copy of those attributes into your project, or reference the ReSharper annotations assembly. Once you've done that, you can tag MyMethod like this:

[ContractAnnotation("=> halt")]
public void MyMethod()
...

However, again, the C# compiler will not care about this and will still complain about the missing return.

You can read more about the various attributes that ReSharper understands here: ReSharper Code Annotations Attributes.

like image 185
Lasse V. Karlsen Avatar answered Oct 09 '22 09:10

Lasse V. Karlsen


You should not use an exception on a method like this, because exceptions are meant to raise WHEN a method could not achive its goal because of EXCEPTIONAL conditions, not ALWAYS.

You should use a custom exception like

public class MyException():Exception
{ 
    public MyException(): base("Your text goes here")
    {
        // do some logging
    }
}

You could also add properties to your custom exception class, and hold additional information on them like

public class MyException():Exception
{ 
    public long Id { get; set; }

    public MyException(long id): base("Your text goes here")
    {
        this.Id = id;
        // and do some logging
    }
}
like image 23
Pablo Recalde Avatar answered Oct 09 '22 08:10

Pablo Recalde