Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



How to throw a custom exception in .NET?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 11-09-2009
Member
 
Join Date: Feb 2009
Posts: 71
How to throw a custom exception in .NET?
  

I wanted to develop some custom exception for my .NET application, I am aware about the exceptions that has been thrown when the condition false, like this exceptions with my custom message should appear when user unable to satisfy the given conditions.

Reply With Quote
  #2  
Old 11-09-2009
Member
 
Join Date: Dec 2008
Posts: 161
Re: How to throw a custom exception in .NET?

The absolute minimum a new custom exception class needs to have is a name. you need to create a custom exception which is thrown if a login attempt fails. A good name for such an exception would be LoginFailedException. Instead of using the preset exceptions from the System.Exception, you define your OwnException class and inherit Exception or ApplicationException class.
Reply With Quote
  #3  
Old 11-09-2009
Member
 
Join Date: Mar 2008
Posts: 258
Re: How to throw a custom exception in .NET?

Creating your own custom exceptions is very easy and allows your code to be more explicit as well as being able to provide more user-friendly error messages. You need to define three constructors in your OwnException Class: One without parameters, other with String parameter for error message and the last one has to have one parameter as a String and other as an Inner exception object.


class OwnException : ApplicationException
{
public OwnException() : base() {}
public OwnException(string s) : base(s) {}
public OwnException(string s, Exception ae) : base(s, ae) {}
}
Reply With Quote
  #4  
Old 11-09-2009
Member
 
Join Date: Dec 2008
Posts: 202
Re: How to throw a custom exception in .NET?

Structured exception handling was introduced to Visual Basic programmers with the first .NET version of the language. Exception handling semantics in .NET are based on type, so you can create custom exceptions that have their own properties and methods. In .NET, exceptions are first-class citizens, and since they're the built-in error handling mechanism, all .NET-compliant languages must support exceptions. Usually the exceptions that occur in the try are caught in the catch block. The finally is used to do all the cleaning up work.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to throw a custom exception in .NET?"
Thread Thread Starter Forum Replies Last Post
How to create a Custom Capsule that is a Custom Firmware with root on lacie 2big Network drive Kane89 Networking & Security 8 06-05-2012 12:21 PM
No New HD channels ,Should I throw my Tata Sky DTH? DIMINUTIVE! India BroadBand 3 18-11-2011 09:32 PM
Can someone please tell me what i can throw out in treasure Isle %Achan Video Games 4 23-02-2011 04:41 AM
How to Throw Exceptions in Java? Bigga Lexx Software Development 4 18-02-2010 04:57 AM
Difference between throw and throws in Java Exception Handling KALYAN23 Software Development 3 09-11-2009 10:49 PM


All times are GMT +5.5. The time now is 04:46 AM.