user4919555
user4919555

Reputation:

Exception is not getting defined

I am trying to create a new custom exception.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project1.Exception
{
  public class ItemNotFoundException: Exception
  {
  }
}

When building the project it is giving an error.

Error 51 'Solution1.project1.Exception' is a 'namespace' but is used like a 'type' D:\wORKsHOP\Arte.Apax2 08.05.2015\project1\Attributes\Logger.cs 71 80 Solution1.project1

Any idea why it is happening? Do I need to add something like System.exception?

Upvotes: 2

Views: 331

Answers (3)

Ricky Alexandersson
Ricky Alexandersson

Reputation: 94

The name space that you are using is called

namespace project1.Exception

within this namespace you are using an inheritance of Exception. The problem with this is that the compiler thinks that your

public class ItemNotFoundException: Exception 

is refering to the namespace and not the Exception standard class. To fix this, simply write

public class ItemNotFoundException: System.Exception

And it should work just fine.

Upvotes: 1

Marwie
Marwie

Reputation: 3317

You created a name ambiguity by calling your namespace Exception. Now the compiler does consider the namespace naming more important than the object named Exception inside the System namespace. Consequently it warns you about inheriting from a namespace instead of an object. If you want to inherit System.Exception in such a namespace you need to full qualify the inherited objects name including its namespace to resolve the ambiguity.

namespace project1.Exception
{
  public class ItemNotFoundException: System.Exception //Fullqualified reference to exception
  {
  }
}

Of course you can also simply rename your namespace to not contain Exception, as mentionend earlier - then you can live without the fullqualified name.

Upvotes: 0

David Arno
David Arno

Reputation: 43254

Rename namespace project1.Exception to namespace project1.MyCustomException or some such. The compiler is seeing Exception in

public class ItemNotFoundException: Exception

as referring to your namespace, Exception, rather than the class. Renaming your namespace to anything other than Exception will fix this.

Upvotes: 3

Related Questions