locked
How to throw custom exceptions in vb.net? RRS feed

  • Question

  • Hi All,

    I am catching sql exception from databse as shown below.

    try

    catch ex as SqlException

    Throw ex

    End Try.

     

    Now I want to throw exception by adding some userfiendly information,Like below.

     

    Throw SQLexception + "my message.....".

    Can you please tell me how can I do this?

    Regards,

    SJN

    Saturday, September 24, 2011 12:47 PM

Answers

  • HI Mike,

    Thanks for the reply.

     

    But it is saying ' system.data.sqlclient.sqlexception ' has no constructors.

    regards

    sjn


    See this post:

    You cannot directly (you can throw a generic Exception class but cannot SqlException directly) but use built-in "raiseerror" command and throw it as if it's a sqlexception:

    This links shows how to do and see the proof by handling your manual Sqlexception you've thrown:

    http://smartypeeps.blogspot.com/2006/06/how-to-throw-sqlexception-in-c.html

    VB.NET code might look like this:

      Try
                Dim DBconn As New SqlClient.SqlConnection("connection_string_here")
                ' Here you throw SQLException
                Dim cmd As New SqlClient.SqlCommand("raiserror('Manual SQL exception', 16, 1)", DBConn)
                cmd.ExecuteNonQuery()
            Catch ex As SqlClient.SqlException
                ' msg = "Manual SQL exception"
                Dim msg As String = ex.Message
            End Try

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)



    Saturday, September 24, 2011 1:05 PM
  • Usually this is done by throwing your own Exception (or the base class Exception) providing the original exception as the InnerException:

          Try
             '...
          Catch ex As SqlException
             Throw New Exception("Error trying open database. See innerexception for details.", ex)
          End Try
    
    


    Armin
    Monday, September 26, 2011 8:32 PM

All replies

  • Like this:
    Throw New SqlException("my message...")
     

    --
    Mike
    Saturday, September 24, 2011 12:52 PM
  • HI Mike,

    Thanks for the reply.

     

    But it is saying ' system.data.sqlclient.sqlexception ' has no constructors.

    regards

    sjn

    Saturday, September 24, 2011 12:58 PM

  • Ah, you are correct.  Sorry about that. 
     
    I would throw a DBException, or just a generic Exception from your code.  Both those classes have constructors as I described.

    --
    Mike
    Saturday, September 24, 2011 1:05 PM
  • HI Mike,

    Thanks for the reply.

     

    But it is saying ' system.data.sqlclient.sqlexception ' has no constructors.

    regards

    sjn


    See this post:

    You cannot directly (you can throw a generic Exception class but cannot SqlException directly) but use built-in "raiseerror" command and throw it as if it's a sqlexception:

    This links shows how to do and see the proof by handling your manual Sqlexception you've thrown:

    http://smartypeeps.blogspot.com/2006/06/how-to-throw-sqlexception-in-c.html

    VB.NET code might look like this:

      Try
                Dim DBconn As New SqlClient.SqlConnection("connection_string_here")
                ' Here you throw SQLException
                Dim cmd As New SqlClient.SqlCommand("raiserror('Manual SQL exception', 16, 1)", DBConn)
                cmd.ExecuteNonQuery()
            Catch ex As SqlClient.SqlException
                ' msg = "Manual SQL exception"
                Dim msg As String = ex.Message
            End Try

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)



    Saturday, September 24, 2011 1:05 PM
  • Onur,
     
    I never knew about that one!  Much better!

    --
    Mike
    Saturday, September 24, 2011 1:11 PM
  • HI ONUR,

    Thanks for the reply.

     I have to throw the exception.

    after this   ==> Dim msg As String = ex.Message

    Throw msg is giving error.

    Regards

    sjn

    Saturday, September 24, 2011 1:13 PM
  • HI ONUR,

    Thanks for the reply.

     I have to throw the exception.

    after this   ==> Dim msg As String = ex.Message

    Throw msg is giving error.

    Regards

    sjn


    You shouldn't use "throw" keyword. You should throw sqlexception indirectly by executing SqlCommand (raiserror) when ExecuteNonQuery is called. Use the provided code-block as is, and it demonstrates the proof of handling sqlexception which you've just thrown.

    HTH.


    Best regards, Saygılarımla, Onur Güzel

    Yazgeliştir Forumları VB.NET / C# Süper Moderatorü.

    Microsoft Haber Grupları Profilim (VB.NET)

    Saturday, September 24, 2011 4:41 PM
  • Usually this is done by throwing your own Exception (or the base class Exception) providing the original exception as the InnerException:

          Try
             '...
          Catch ex As SqlException
             Throw New Exception("Error trying open database. See innerexception for details.", ex)
          End Try
    
    


    Armin
    Monday, September 26, 2011 8:32 PM