0

I'm developing a web project where i need to add custom exception classes. For example, how can i display a message from my custom exception class when the session timeout occurs? Please help. Any sample will be helpful.

This is what i written in my exception class so far:

public class CustomException : Exception
{
    private string message;

    public CustomException ()
    {
        this.message = "Invalid Query";
    }
    public CustomException (String message)
    {
        this.message = message;

    }

}

Need to know how to link this with the session timeout, from where i need to write the logic of the same. Thank You.

NewBie
  • 1,824
  • 8
  • 35
  • 66
  • why would you want to throw a custom exception when session timeout occurs? – Eranga Jul 14 '11 at 06:37
  • Im trying to return bulk data on executing a query from a layer A and it returns null on session time out on layer A . It's wont show any error on layer A. So i need to use custom exception for that. – NewBie Jul 14 '11 at 07:30

4 Answers4

1

If you are looking to throw your custom exception when the is raised you can do it like this.

try {
    DataTable dt = q.ExecuteQuery(); //This throws a timeout.
} catch(SessiontTimeoutException ste) {
    throw new CustomException("Session has timed out");
} catch(Exception e) {
    //Show unexpected exception has occured
}

Not too sure if this is what you are trying to do.
Update:
To Find out if SqlException is a TimeoutException please see this StackOverFlow Post.

Community
  • 1
  • 1
Jethro
  • 5,896
  • 3
  • 23
  • 24
  • You should (almost) always include the original exception as the InnerException, too: `throw new CustomException("Session has timed out", ste)` – Dan Puzey Jul 14 '11 at 06:57
  • @Dan Puzey, agreed. The more info you have about the problem the better. – Jethro Jul 14 '11 at 07:18
  • What is this SessionTimeoutException? Couldnt find a directive for the same. – NewBie Jul 14 '11 at 07:25
  • ?? I took it from your post. Were you talking about TimeoutException, or SQL TimeoutException or the actual Asp.net session Timeout exception? You get quite a few. – Jethro Jul 14 '11 at 07:29
  • System.Data.SqlClient.SqlException: Timeout expired. – NewBie Jul 14 '11 at 07:31
  • Ok, so you are getting a SqlException, are you trying to determine when it is a TimeOutException? – Jethro Jul 14 '11 at 07:34
0

You might want to write this as

public CustomException() : base("Invalid Query") { }

this way the exception message gets passed correctly, for the other constructor

public CustomException(String message) : base(message) { }

then you don't need the private string message field.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • Thank you for the response. If i'm writing a code like dt=q.ExecuteQuery(); within a try block how my exception class knows whether the exception thrown is a session time out exception,inorder to display the message. – NewBie Jul 14 '11 at 06:33
  • If it's a custom exception you should be throwing it yourself, you should let ExecuteQuery() (unless you wrote it yourself) throw the appropriate exception. If it is thrown by you simply catch (CustomException e) – Jesus Ramos Jul 14 '11 at 06:35
  • That means. I have to check the session time out within my application code itself...Not within the exception class. – NewBie Jul 14 '11 at 06:37
  • Yes you should catch the SessionTimeoutException or whatever gets called by placing it as the "argument" to the catch – Jesus Ramos Jul 14 '11 at 06:39
0

SqlException class exposes the property Errors which is a collection of SqlError objects. You can query the Number property of each error object which corresponds to an entry in the master.dbo.sysmessages table.

RePierre
  • 9,358
  • 2
  • 20
  • 37
0

I recommend you use Inner Exception to get user friendly exception message with also the system error message. If getting MyException, you'll see your exception message and the system exception message at MyException.ToString().

Additionally, if you are concerned for coding Exception, you can use the Code Snippet Feature of VS. Just type 'Exception' and press TAB key twice, then VS will create Exception class as the following code.

try
{
    DataTable dt = q.ExecuteQuery(); //This throws a timeout.
}
catch (SessiontTimeoutException ex)
{
    throw new MyException("my friendly exception message", ex);
}

[Serializable]
public class MyException : Exception
{
    public MyException() { }
    public MyException(string message) : base(message) { }
    public MyException(string message, Exception inner) : base(message, inner) { }
    protected MyException(
      System.Runtime.Serialization.SerializationInfo info,
      System.Runtime.Serialization.StreamingContext context)
        : base(info, context) { }
}
Jin-Wook Chung
  • 4,196
  • 1
  • 26
  • 45