Results 1 to 4 of 4

Thread: How to throw a custom exception in .NET?

  1. #1
    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.

  2. #2
    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.

  3. #3
    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) {}
    }

  4. #4
    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.

Similar Threads

  1. Replies: 8
    Last Post: 06-05-2012, 12:21 PM
  2. No New HD channels ,Should I throw my Tata Sky DTH?
    By DIMINUTIVE! in forum India BroadBand
    Replies: 3
    Last Post: 18-11-2011, 10:32 PM
  3. Replies: 4
    Last Post: 23-02-2011, 05:41 AM
  4. How to Throw Exceptions in Java?
    By Bigga Lexx in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 05:57 AM
  5. Difference between throw and throws in Java Exception Handling
    By KALYAN23 in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 11:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,424,224.81588 seconds with 17 queries