Where to put try / catch in 3-tier architecture

I have a 3 level web application. I want to use. I use a catch catch block at my business logic level. Is it right to use a try / catch block in business logic or do I need to use it in my user interface layer?

see my code for DAL.

Data Access Layer

#region Insert in to Logbook
public int Insert_LogBook(string Vehicle_Number, DateTime Vehicle_Booking_Date, TimeSpan Time_From, TimeSpan Time_To, int KM_Start, int KM_End, string Vehicle_Used_By, string Cost_Code, string Budget_Line, DateTime Entry_Date)
{
    try
    {
        SqlCommand com = new SqlCommand("Insert_LogBook", con);
        com.Parameters.Add("@Vehicle_Number", SqlDbType.NVarChar, 100).Value = Vehicle_Number;
        com.Parameters.Add("@Vehicle_Booking_Date", SqlDbType.DateTime).Value = Vehicle_Booking_Date;
        com.Parameters.Add("@Time_From", SqlDbType.Time).Value = Time_From;
        com.Parameters.Add("@Time_To", SqlDbType.Time).Value = Time_To;
        com.Parameters.Add("@KM_Start", SqlDbType.Int).Value = KM_Start;
        com.Parameters.Add("@KM_End", SqlDbType.Int).Value = KM_End;
        com.Parameters.Add("@Vehicle_Used_Byr", SqlDbType.VarChar, 100).Value = Vehicle_Used_By;
        com.Parameters.Add("@Cost_Code", SqlDbType.NVarChar, 50).Value = Cost_Code;
        com.Parameters.Add("@Budget_Line", SqlDbType.NVarChar, 50).Value = Budget_Line;
        com.Parameters.Add("@Entry_Date", SqlDbType.DateTime).Value = Entry_Date;
        con.Open();
        int res = com.ExecuteNonQuery();

    }
    catch (Exception ex)
    {
        WebMsgBox.Show(ex.Message);
    }
    finally
    {
        con.Close();
        con.Dispose();

    }
    return 1;
}
#endregion

So I have to use it in my layer bal OR IN IN or my code is fine. because if I do not use try / catch in my user interface layer, it will not throw an exception (if any) and shows the error page.

+5
source share
4 answers

Exception handling and metaling are what I see as misunderstood all the time, by most of the developers I worked with.

  • .
  • , "" .
  • ( ) , NullReferenceException.
  • , .
  • , , catch, wrap throw.

try/catch/() , ...

catch , , . - . //.

, , InnerException.

. , DataAccessException , , InnerException. , , , , , DataAccessException - .

DataAccessException , SqlDataAccessException SecurityDataAccessException.

, , , , - , . , , try/ .

, , try/catch , , .

: " , , ?" ExecuteNonSql, API/ , . XML- , DLL, XML.

, . /- , , , :)

Cwalina Abrams. , , , API Microsoft ( ).

. .

" { }. { }. { }. . { | }."

, :

...
catch(FileNotFoundException fnfe)
{
    string m = String.Format("Cannot save changes. A FileNotFoundException occurred. Check the path '{0}' is valid, that your network is up, and any removable media is available. Please see inner exception.", path);

    _log.Error(m, fnfe);

    throw new StorageLifecycleException(m, fnfe);
}
+3

, try catch .

, ; , . exception, , SqlException .

  • , ( catch SqlException, )

  • ( )

  • , ; DAL DAL, UI .

+4

try-catch, . , " ". , , :

catch (Exception ex)
{
    WebMsgBox.Show(ex.Message);
}

:

- , , ( ). . , try-catch, , .

public int Insert_LogBook(string Vehicle_Number, DateTime Vehicle_Booking_Date, TimeSpan Time_From, TimeSpan Time_To, int KM_Start, int KM_End, string Vehicle_Used_By, string Cost_Code, string Budget_Line, DateTime Entry_Date)
{
    using(SqlCommand com = new SqlCommand("Insert_LogBook", con))
    {
        com.Parameters.Add("@Vehicle_Number", SqlDbType.NVarChar, 100).Value = Vehicle_Number;
        com.Parameters.Add("@Vehicle_Booking_Date", SqlDbType.DateTime).Value = Vehicle_Booking_Date;
        com.Parameters.Add("@Time_From", SqlDbType.Time).Value = Time_From;
        com.Parameters.Add("@Time_To", SqlDbType.Time).Value = Time_To;
        com.Parameters.Add("@KM_Start", SqlDbType.Int).Value = KM_Start;
        com.Parameters.Add("@KM_End", SqlDbType.Int).Value = KM_End;
        com.Parameters.Add("@Vehicle_Used_Byr", SqlDbType.VarChar, 100).Value = Vehicle_Used_By;
        com.Parameters.Add("@Cost_Code", SqlDbType.NVarChar, 50).Value = Cost_Code;
        com.Parameters.Add("@Budget_Line", SqlDbType.NVarChar, 50).Value = Budget_Line;
        com.Parameters.Add("@Entry_Date", SqlDbType.DateTime).Value = Entry_Date;
        con.Open();
        int res = com.ExecuteNonQuery();

        return 1;
    }

}

public void SomeMethodWhichUsesThatInsert()
{
    try
    {
        //call Insert_LogBook
    }
    catch(SomeException e)
    {
        //handle
    }

}
+2
source

You must put your data access and business logic in class libraries. You must not name front end components from BLL or DAL. Create logging in your BLL and DAL using this data, such as error data in log4net.

then throw the exception up if you want to notify users of the error status.

0
source

All Articles