Help!

Hello! Can anyone help me? I am in a class, and quite confused about what to do with this procedure. Does anyone have any advice?

write a sub procedure that can be used as a generic error-handling routine. The procedure should take two arguments, a string describing where the handler is being called and an exception object. The procedure would normally be called in the Catch section of a Try block, so that you can code all of your procedures or functions with the same structure, a single Try block around all of your code. The procedure should append the exception's message text to the handler description and reraise the exception with the newly concatenated message. Essentially, this approach would show a stack dump of called procedures/methods for a failed piece of code, like many Microsoft provided error messages, for example, "[routinex][routiney][ado.open]you made an error".

Any help or explanation is greatly appreciated! Thanks


Comments

  • [b][red]This message was edited by infidel at 2005-7-25 8:53:42[/red][/b][hr]
    : Hello! Can anyone help me? I am in a class, and quite confused about what to do with this procedure. Does anyone have any advice?
    :
    : write a sub procedure that can be used as a generic error-handling routine. The procedure should take two arguments, a string describing where the handler is being called and an exception object. The procedure would normally be called in the Catch section of a Try block, so that you can code all of your procedures or functions with the same structure, a single Try block around all of your code. The procedure should append the exception's message text to the handler description and reraise the exception with the newly concatenated message. Essentially, this approach would show a stack dump of called procedures/methods for a failed piece of code, like many Microsoft provided error messages, for example, "[routinex][routiney][ado.open]you made an error".
    :
    : Any help or explanation is greatly appreciated! Thanks

    This seems like a stupid assignment for a whole host of reasons. Not only stupid but dangerous because it is teaching new programmers to do stupid things, things that are stupid on a number of different levels. Here's the kind of procedure you're being asked to write:

    [code]
    Module Module1

    Public Sub MyStupidErrorHandler(ByVal Source As String, ByVal OriginalException As Exception)

    OriginalException.Source = Source & "->" & vbCrLf & OriginalException.Source

    Throw OriginalException

    End Sub

    End Module
    [/code]

    It turns out that Exception.Message is a ReadOnly property, so you can't append anything to it, you'd have to use the Source property instead, which is just stupid because Exception objects have a StackTrace property that lists the execution stack for you. As I read your requirements, here is how you would use this procedure (in every single procedure/function you write):

    [code]
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Try
    'code here
    Catch ex As Exception
    MyStupidErrorHandler("Form1_Load", ex)
    End Try

    End Sub
    [/code]

    There are tons of stupid things wrong with coding like this. Not the least of which is that you have to type out the name of every procedure as a string literal so the stupid error handler procedure knows what to append to the Source. It also completely eliminates any benefit you receive from having typed exceptions in the first place. Third, I haven't tried it, but I'll wager that it's going to bloat the real stack trace of the exception object with calls to the stupid error handler routine interspersed with calls to your actual code. Fourth, you're actually losing information with exception handling like this because it focuses on which procedure raised an exception rather than which statement within that procedure. Lastly, it's encouraging people to think of Exceptions the same way that VB6 errors were handled, which is perhaps the reason for this assignment being so messed up in the first place.

    Ok, so I can see how this might be a trivial example to demonstrate the concepts of exceptions and call stacks, blah blah blah, but you shouldn't be introducing bad ideas to your students in your trivial assignments, because the bad ideas are going to influence later decisions.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]



  • [b][red]This message was edited by ldn769 at 2005-7-25 17:37:26[/red][/b][hr]
    Well I am glad that I am not the only one who thinks that projects like this is stupid!! Thank you for your help! I suppose it is a good thing that I don't know how to do it too, seeing as how it would just be wrong to learn like that!

    : [b][red]This message was edited by infidel at 2005-7-25 8:53:42[/red][/b][hr]
    : : Hello! Can anyone help me? I am in a class, and quite confused about what to do with this procedure. Does anyone have any advice?
    : :
    : : write a sub procedure that can be used as a generic error-handling routine. The procedure should take two arguments, a string describing where the handler is being called and an exception object. The procedure would normally be called in the Catch section of a Try block, so that you can code all of your procedures or functions with the same structure, a single Try block around all of your code. The procedure should append the exception's message text to the handler description and reraise the exception with the newly concatenated message. Essentially, this approach would show a stack dump of called procedures/methods for a failed piece of code, like many Microsoft provided error messages, for example, "[routinex][routiney][ado.open]you made an error".
    : :
    : : Any help or explanation is greatly appreciated! Thanks
    :
    : This seems like a stupid assignment for a whole host of reasons. Not only stupid but dangerous because it is teaching new programmers to do stupid things, things that are stupid on a number of different levels. Here's the kind of procedure you're being asked to write:
    :
    : [code]
    : Module Module1
    :
    : Public Sub MyStupidErrorHandler(ByVal Source As String, ByVal OriginalException As Exception)
    :
    : OriginalException.Source = Source & "->" & vbCrLf & OriginalException.Source
    :
    : Throw OriginalException
    :
    : End Sub
    :
    : End Module
    : [/code]
    :
    : It turns out that Exception.Message is a ReadOnly property, so you can't append anything to it, you'd have to use the Source property instead, which is just stupid because Exception objects have a StackTrace property that lists the execution stack for you. As I read your requirements, here is how you would use this procedure (in every single procedure/function you write):
    :
    : [code]
    : Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    :
    : Try
    : 'code here
    : Catch ex As Exception
    : MyStupidErrorHandler("Form1_Load", ex)
    : End Try
    :
    : End Sub
    : [/code]
    :
    : There are tons of stupid things wrong with coding like this. Not the least of which is that you have to type out the name of every procedure as a string literal so the stupid error handler procedure knows what to append to the Source. It also completely eliminates any benefit you receive from having typed exceptions in the first place. Third, I haven't tried it, but I'll wager that it's going to bloat the real stack trace of the exception object with calls to the stupid error handler routine interspersed with calls to your actual code. Fourth, you're actually losing information with exception handling like this because it focuses on which procedure raised an exception rather than which statement within that procedure. Lastly, it's encouraging people to think of Exceptions the same way that VB6 errors were handled, which is perhaps the reason for this assignment being so messed up in the first place.
    :
    : Ok, so I can see how this might be a trivial example to demonstrate the concepts of exceptions and call stacks, blah blah blah, but you shouldn't be introducing bad ideas to your students in your trivial assignments, because the bad ideas are going to influence later decisions.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    : [code]
    : $ select * from users where clue > 0
    : no rows returned
    : [/code]
    :
    :
    :
    :



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion