Link to home
Get AccessLog in
Avatar of Ron Kidd
Ron KiddFlag for Australia

asked on

Exception Handling in a N tier application visual basic 2015

I'm moving from vba to vb.net.
I have a 3 tier application - UI, BLL and DAL.

How is the best way to handle Exceptions?

I Know how to catch and throw them.
I have set up a Centralised Exception handler

At what point should I log the Exceptions?
Where they are thrown or in the UI?
If Where they are thrown then how do the Upper Layers know that the exception has been logged so I don't get duplicates?

It seems far easier to do all the Logging in the UI, This is being done by calling a Shared Sub in the BLL.
Is this Bad Practice?

I am looking to log the exceptions in the database and if that fails (No Connection to Database) then write to a Text file Log.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Avatar of Ron Kidd

ASKER

Thanks
So do you display a message box from the BLL?
Or how do you get the Message from the BLL to the UI?

Do you still have Try...Catch in the UI at all?
Also a Good point about Switching UI as I will be changing from winforms to WPF in the near future
SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
Sorry - i should have tested the Link I pasted.

So with the centralized Exception Handling I don't need to have Try... Catch everywhere in the UI (Only where I can deal with the exceptions and move on) as any Unexpected Exceptions will be Handled by this  Handler.

kaufmed
What do you mean by a property to hold error messages?
Below is a sample I have that produces an error by calling a Database procedure that doesn't exist

It checks the Error for either a Transaction Deadlock or a String Truncation error - It handles these then records and throws for all other errors

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            DatabaseErrorTestBLL.Save(25, "NoName")
        Catch ex As SqlException
            If ex.Number = 1205 Then
                MessageBox.Show("Transaction Deadlock. Please Re-run the Transaction.")
            ElseIf ex.Number = 6234 Or ex.Number = 7720 Or ex.Number = 8152 Or ex.Number = 35478 Then
                MessageBox.Show("Data would be Truncated. Please check the Length of the Data and Re-run the Transaction")
            Else
                MessageBox.Show("Connection to the Database has been Lost. The Application will now close")
                ExceptionRecorder.NewRecord(ex)
                Throw
            End If
        Catch ex As Exception
            MessageBox.Show("Connection to the Database has been Lost. The Application will now close")
            Throw
        End Try

    End Sub

Open in new window


The Save Sub that throws the Error
Public Class DatabaseErrorTestBLL
    Shared Sub Save(ByVal staffNo As Int32, ByVal name As String)
        Try
            DataBaseError.Save(staffNo, name)
        Catch ex As Exception
            Throw
        End Try
    End Sub
End Class

Open in new window


I then Record the Error with
Imports System.Data.SqlClient
Public Class ExceptionRecorder
    Shared Sub NewRecord(Ex As Exception)
        ExceptionRecorderData.save(Ex)
    End Sub

    Shared Sub NewRecord(Ex As SqlException)
        ExceptionRecorderData.save(Ex)
    End Sub
End Class

Open in new window


Where would I add a Property and How Check it?

Thanks All