Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing exceptions from DLL to ASPX page?

Status
Not open for further replies.

NBartomeli

Programmer
Jul 1, 2003
111
US
I have a dll (report.vb) file that i pass a dataset to so it will get filled. In the routine, if the dataset exceedes a certain number of rows or columns, I want it to throw an exception and exit before it goes back to the page.

I have the following code

reports.vb:
Code:
if rows > 255 or cols > 255 then
     Throw New System.Exception("Rows / Cols exceeded")
endif

report.aspx:
Code:
try
  dsReportData = buildReportData()
catch ex as exception
   'handle exception
finally
   ...
end try

When I step through the code, it sees the exception, then just continues on with the function and is never caught. Am I doing something wrong?

Thanks in advance for any help.
 
You don't necessarily have to throw an exception - you could do something very similar using a boolean value. For Example,
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If DoSomething(300) = True Then
            ' the number was too large
        Else
            ' the number was OK
        End If
    End Sub

    Private Function DoSomething(ByVal MyValue As Integer) As Boolean
        If MyValue > 255 Then
            Return True
        Else
            Return False
        End If
    End Function

Obviously I'm not looping through a DataSet but it should give you an example of what I'm trying to explain.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
I appreciate your input,

However, the function must return a dataset, so I cannot have it return a boolean at the same time

I am also interested in learning how exceptions are used in this case.

thanks anyway, I appreciate it.
 
Hmmm - I tried a simple example:
Code:
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Try
            DoSomethingElse()
        Catch ex As Exception
            Response.Write(ex.StackTrace)
        End Try

    End Sub
    Private Function DoSomethingElse()
        Dim i As Integer = 1
        Throw New System.Exception("Rows / Cols exceeded")
        Dim j As Integer = 1
    End Function
When my exception is thrown, it exits the function, writes out the error and therefore "j" doesn't get set.

Are you saying that yours doesn't have this behaviour?

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Yes, thank you

My trouble was I was trying to throw an exception back from the function inside a try/catch block. So the exception was never leaving the function, it was getting caught in the function itself and not returning to the page.

I fixed it, thank you for your input.

 
Ahh I see - it's nearly always (sometimes it isn't) best practice to have your Try/Catch in the calling function as opposed to the function itself then you can catch those errors and you know that an error occured in the function - otherwise, as you found out, the error will just be handled and your original function will carry on as normal.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Yeah. That is the best way, but I called the function from the ASPX page, which had a try-catch block to trap errors in the functions that it called. (does that make sense?) I just ended up having too many functions calling too many functions with 1 too many try-catch blocks in the mix.

I think I need to re-work my logic :-D

Thanks again

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top