locked
try / catch as RRS feed

  • Question

  • I use

     

    Try

     

    Catch ex As Exception

     

    End Try

     

    a few times in my code it seems so much easier than coding error checks should I be doing this ?  is it reliable ?

     

     

     

    Monday, November 24, 2008 10:15 PM

Answers

  • "seems so much easier than coding error checks should I be doing this"

     

    No!  You should be checking for all errors that you know can affect the operation of your program and that you can handle.  Use Try, End Try to catch errors thrown by the system that you can't handle. 

    Tuesday, November 25, 2008 6:53 PM
  •  Programble (VBVB6) wrote:
    Do you mean, no matter if the code was executed successfully or not? If so, why not just put code below the Try...Catch...End Try block?

     

    A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

    Tuesday, November 25, 2008 11:56 PM

All replies

  • Hi Dayjur.

     

    We do use ( Try, Catch, Finally, End Try ), when we're not able to ( Find a solution that can fix the errors ) .

     

    ' 4 Example : You can't get the files which are into ( System Volume Information )'s Folder, so

    If you use ( Try, Catch Ex as Exception, End Try )  to Get the files which are intro the ( System Volume Information )'s Folders ( Into Your HD Drivers ), You'll have many Errors ( Exceptions ) <> Fixed By :

     

    Code Snippet

    If Not DirectoryName = "System Volume Information" Then

    AddItem( DirectoryName, GetFiles( DirectoryFullName ) )

    Else

    AddItem( DirectoryName )

    End If

     
     
    If you have other questions, please write a code to let me tell you ( If you should use ( try, catch ) or you shouldn't )
     
    Best Regards,
     
    BVS.
    Tuesday, November 25, 2008 12:24 AM
  • I'm not sure how to respond. You 'should' be using Try Catch blocks around all code that might throw an exception. OnError GOTO is outdated.

     

    A.D.T.

     

    Tuesday, November 25, 2008 2:59 AM
  •  

    well one example of using try in my app which i use a few times is

     

    getting a date value from a dataset

    for example

     

    Dim rst As Date

     

    Try

    rst = TC_Users.Tables("TCusers").Rows(n1).Item("UserDate")

    Catch ex As Exception

    rst = Nothing

    End Try

     

    I use this because if date field contains nothing you get an excpetion and i searched for a workaround

    but did not find much and what I did find were outdated and did not work

    however i did find a little trick in that

     

    if you let a varaible =""+ ( the nothing value from database ) if there is a nothing value than no excpetion thrown and variable simply =""

    but that seems a little unorthodox

     

     

     

    Tuesday, November 25, 2008 3:26 AM
  • In your specific example, there are tons of built-in functions that will validate a date. IsDate() is one function to begin with.

     

    A.D.T.

     

    Tuesday, November 25, 2008 3:38 AM
  •  

    Hi Dayjur .

     

    You've got two solutions to fix that prob >

     

    1st -

     

    Code Snippet

    Dim RST As Date

     

    If Not TC_Users.Tables("TCusers").Rows(n1).Item("UserDate") Is Nothing Then

    RST =  TC_Users.Tables("TCusers").Rows(n1).Item("UserDate")

    Else

    RST = Now

    End If

     

     

    2nd -

     

    Code Snippet

    Dim RST As Date

                     ' Byval                                              ' Byref

    If Date.TryParse(TC_Users.Tables("TCusers").Rows(n1).Item("UserDate") , RST ) Then

    Msgbox( "It was a valid date")

    Else

    Msgbox( "It wasn't a valid date")

    End If

     

    Best Regards,

     

    BVS.

    Tuesday, November 25, 2008 1:29 PM
  •  BeforeAndAfter1974 wrote:

    I'm not sure how to respond. You 'should' be using Try Catch blocks around all code that might throw an exception. OnError GOTO is outdated.

     

    A.D.T.

     

     

    try/catch should not be a substitute for good coding skills. Please read

    http://vbdotnetforum.com/index.php?showtopic=397

    Tuesday, November 25, 2008 2:21 PM
  •  BeforeAndAfter1974 wrote:

    I'm not sure how to respond. You 'should' be using Try Catch blocks around all code that might throw an exception. OnError GOTO is outdated.

     

    In general I agree with this advice, but it depends on where the code is.  If the code is in a class library for example, you should not catch the exception unless you can actually handle it properly.  Otherwise, it should be allowed to bubble up to any handlers up the call stack.  As a last line of defense, the app should have some method for taking care of any exceptions not handled anywhere else.  If the app cannot handle the exception at this point, then displaying a user friendly error message and closing the app gracefully is the normal way to go. 

     

    I also think that you should have more try..finally blocks than try..catch blocks, and the using keyword should be used where appropriate.   

     

    Chris

    Tuesday, November 25, 2008 6:38 PM
  • "seems so much easier than coding error checks should I be doing this"

     

    No!  You should be checking for all errors that you know can affect the operation of your program and that you can handle.  Use Try, End Try to catch errors thrown by the system that you can't handle. 

    Tuesday, November 25, 2008 6:53 PM
  •  

    Ok tx for the date examples and prob John Wayne is right I will make the extra effort cheersSmile

     

    Tuesday, November 25, 2008 7:46 PM
  • On the topic on Try, I have used Try...Catch...End Try, but I have never used Finally. What is it's purpose?
    Tuesday, November 25, 2008 10:17 PM
  • It means that it will be called no matter what.

     

    Tuesday, November 25, 2008 10:21 PM
  • Do you mean, no matter if the code was executed successfully or not? If so, why not just put code below the Try...Catch...End Try block?
    Tuesday, November 25, 2008 10:23 PM
  • It is used to clean up any resources. Dispose of IO readers/Writers, Close DB connections, etc..

    Tuesday, November 25, 2008 10:32 PM
  •  Programble (VBVB6) wrote:
    Do you mean, no matter if the code was executed successfully or not? If so, why not just put code below the Try...Catch...End Try block?

     

    A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

    Tuesday, November 25, 2008 11:56 PM