Help handling exceptions

I have this application structure: 1. Presentation level, which calls 2. Business Logic Layer, which in turn calls 3. Data access level for working with the database.

Now I have a Contact page, from where I can add a new contact to the database.

So, to add a new contact, fill in all the necessary data, and then call the Add method (located in the BLL) from the page, which, in turn, calls the Add method, which is in the DAL.

this method in the DAL returns the current identification of the record, which then returns to the BLL method and finally gets delivered to the page.

this is normal. but what if I get an exception, how I handle it correctly, because the method in DAL has an int return type and I don’t want to throw another error! In other words, I will have to write try catch in almost all methods.

//something like this
public int AddMethod(ContactClass contactObj)
{
    int result = 0;

    try
    {
        result = ExecuteSomeSP("SPName", SP_Parameters);
    }
    catch(SQLException se)
    {
        throw new SQLException
    }

    return result;
}

rather, I want to show the user a user-friendly message that they can easily understand, and while I send the letter to myself, I document the error message that just happened.

Also kindly tell me how I can implement my own exception classes.

Please tell me how to do this?

thank.

+3
source share
2 answers

, . try . .

DAL, DalException SQLException

public int addMethod(ContactClass contactObj) throws DalException {
    try {
        return ExecuteSomeSP("SPName", SP_Parameters);
    }
    catch(SQLException e) {
        throw new DalException(e);
    }
}

-

public void addMethod(ContactClass contactObj) {
    try {
        dal.addMethod(contactObj);
    }
    catch(DalException e) {
        // notify user
        view.alert(e.getMessage());
    }
}
+1

try/catch . / ( ).

, , ..

" " - - , .

, , , InnerException:

try
{
   // DAL
}
catch (DALException de)
{
   // Log, ....

   throw new BLLException(message, de);
}
+3

Source: https://habr.com/ru/post/1752797/


All Articles