[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:
Public Sub AddEmail(ByVal mail As String)
MailEntity.To.Add(New MailAddress(mail))
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.
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.
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:
Public Sub AddEmail(ByVal mail As String)
Try
MailEntity.To.Add(New MailAddress(mail))
Catch ex as Excetpion
End Sub
or in my main application where i create the instance.
dim a as new class
try
a.AddEmail()
catch ex as exception.
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:
Public Sub AddToAddress(ByVal address As String)
Try
MailEntity.To.Add(New MailAddress(address))
Catch sysEx As Exception
Dim appEx As New AddAddressException
appEx.InnerException = sysEx
Throw appEx
End Try
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.
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)
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?