locked
Ask the meanig of "throw(ex)" in try catch RRS feed

  • Question

  • User-318218893 posted

    Hello everybody

    i want to ask the meaning of throw(ex) in try catch

    for example : 

      Try                      
                My.Computer.FileSystem.DeleteDirectory(Server.MapPath("Upload/Temp/"), FileIO.DeleteDirectoryOption.DeleteAllContents)
                Directory.CreateDirectory(Server.MapPath("Upload/Temp"))
            Catch ex As Exception
                Throw (ex)
            End Try

    And what the means My.Computer.FileSystem.DeleteDirectory and not use Directory.DeleteDirectory. The result is same. What is different?

    Thank you very much..
     

    Saturday, April 19, 2008 12:43 PM

Answers

  • User-417784260 posted

     Usually you have this type of code in an subroutine or component.  Basically this code block would catch an exception that is thrown when you try to delete or create a directory and passes it back to the line of code that is calling the subroutine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2008 1:15 PM
  • User-158764254 posted

      Try                      
                My.Computer.FileSystem.DeleteDirectory(Server.MapPath("Upload/Temp/"), FileIO.DeleteDirectoryOption.DeleteAllContents)
                Directory.CreateDirectory(Server.MapPath("Upload/Temp"))
            Catch ex As Exception
                Throw (ex)
            End Try

    In this example, the exception from the Create/Delete is being caught, but nothing is being done with that exception - it is simply rethrown to the calling code.  For all practical purposes, it's almost as if there were no try/catch code added at all as the error will still get thrown. 

    But...  By adding :  Catch ex As Exception  and Throw(ex) ,   the stack is being altered.

    Since this catch in this example is not actually doing anything, it does not need to be there.  When you do need to catch an error but wont be interacting with the Exception object itself, you can also use this syntax.

    Try                       
       My.Computer.FileSystem.DeleteDirectory(Server.MapPath("Upload/Temp/"), FileIO.DeleteDirectoryOption.DeleteAllContents)
       Directory.CreateDirectory(Server.MapPath("Upload/Temp"))
    Catch 
       ' do something here to respond to the error condition
       ' otherwise theres no value in the Catch
       Throw
    End Try 
      

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2008 5:39 PM

All replies

  • User-417784260 posted

     Usually you have this type of code in an subroutine or component.  Basically this code block would catch an exception that is thrown when you try to delete or create a directory and passes it back to the line of code that is calling the subroutine.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2008 1:15 PM
  • User-158764254 posted

      Try                      
                My.Computer.FileSystem.DeleteDirectory(Server.MapPath("Upload/Temp/"), FileIO.DeleteDirectoryOption.DeleteAllContents)
                Directory.CreateDirectory(Server.MapPath("Upload/Temp"))
            Catch ex As Exception
                Throw (ex)
            End Try

    In this example, the exception from the Create/Delete is being caught, but nothing is being done with that exception - it is simply rethrown to the calling code.  For all practical purposes, it's almost as if there were no try/catch code added at all as the error will still get thrown. 

    But...  By adding :  Catch ex As Exception  and Throw(ex) ,   the stack is being altered.

    Since this catch in this example is not actually doing anything, it does not need to be there.  When you do need to catch an error but wont be interacting with the Exception object itself, you can also use this syntax.

    Try                       
       My.Computer.FileSystem.DeleteDirectory(Server.MapPath("Upload/Temp/"), FileIO.DeleteDirectoryOption.DeleteAllContents)
       Directory.CreateDirectory(Server.MapPath("Upload/Temp"))
    Catch 
       ' do something here to respond to the error condition
       ' otherwise theres no value in the Catch
       Throw
    End Try 
      

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2008 5:39 PM
  • User-1614311312 posted

    the throw(ex) statement throws an exception.

    Sunday, April 20, 2008 2:52 AM