Results 1 to 6 of 6

Thread: [2008] class exception vs in app exception.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    [2008] class exception vs in app exception.

    I have a question about some thoery.

    I have a class Z. I have a function in there that calls a .net function. More specifcally i have:

    VB.NET Code:
    1. Public Sub AddEmail(ByVal mail As String)
    2.         MailEntity.To.Add(New MailAddress(mail))
    3. End Sub

    now we know that creating a new instance of the mailaddress with a string parameter can throw 3 exceptions.

    I want to know whter I should put a try catch block in here! OR!!
    When I declare the whole class in my Application (or any other application), I should place the try catch when I call the function..

    I hope I made sense.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] class exception vs in app exception.

    That really depends on your design. Probably the best course of action would be to define your own exception type and only throw that one type of exception from that code. If an exception was thrown you would then catch it, create an instance of your own exception type, assign the original exception to the InnerException property of your own exception, then throw your own exception. That means that calling code can handle one exception type only, but they have access to all the original exception details via the InnerException property.

    That said, it all depends on whether you might want to handle different exceptions differently.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] class exception vs in app exception.

    Alright thanks, i've created my own exception type also.

    but how should I throw exception? For ex:

    Should i wrap it in my class ?
    vb Code:
    1. Public Sub AddEmail(ByVal mail As String)        
    2. Try
    3. MailEntity.To.Add(New MailAddress(mail))
    4. Catch ex as Excetpion
    5. End Sub

    or in my main application where i create the instance.

    dim a as new class
    try
    a.AddEmail()
    catch ex as exception.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] class exception vs in app exception.

    The point of the custom exception is to throw it from your library and then it gets caught in any application referencing that library, e.g.
    vb.net Code:
    1. Public Sub AddToAddress(ByVal address As String)
    2.     Try
    3.         MailEntity.To.Add(New MailAddress(address))
    4.     Catch sysEx As Exception
    5.         Dim appEx As New AddAddressException
    6.  
    7.         appEx.InnerException = sysEx
    8.         Throw appEx
    9.     End Try
    10. End Sub
    The code calling the method will then allow for AddAddressExceptions to be thrown and if it needs more information it interrogates the InnerException.

    Note the more sensible names I've used in my code snippet too. You aren't adding an e-mail so AddEmail is not a logical name for the method. You're adding an address to the recipient list so the method name should reflect that. Also, "mail" is not a logical name for the parameter because it's not mail. It's an e-mail address, so it's name should reflect that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2006
    Posts
    607

    Re: [2008] class exception vs in app exception.

    ok thankyou..
    i have one more question thats unrelated to the orignal..
    does a inner exception also include a message?

    thanks for the help btw, (updated my names)

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] class exception vs in app exception.

    The InnerException IS the exception that you was originally caught by your code. Would you expect that exception to have a Message?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width