0

I have the following code:

    static int GetLastAddedIDHelper(OleDbConnection connection)
    {
        try
        {
            // Init variables.
            OleDbCommand command = null;
            string cmdText = "SELECT @@IDENTITY";

            if (connection != null)
                command = new OleDbCommand(cmdText, connection);
            else
                throw new ArgumentNullException("connection", "The connection was passed as null. Therefore the globally used connection is used but was never set.");

            return (int)command.ExecuteScalar();
        }
        catch (Exception ex) { throw ex; }
    }

    public static int GetLastAddedID()
    {
        try
        {
            return GetLastAddedIDHelper(_Connection);
        }
        catch (Exception ex) { throw ex; }
    }

    private void Button_Click_Action()
    {
        try
        {
            int i = AccessDbServiceBase.GetLastAddedID();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ExceptionHandler.GetExceptionMessage(ex));
        }
    }

The above code will get the last inserted ID from an Access database for me. Now, to do this, Button_Click_Action will call GetLastAddedID and that will call GetLastAddedIDHelper. When a exception occurs in GetLastAddedIDHelper, I throw the exception up to the main method Button_Click_Action.

I'm wondering if I'm doing this the correct way like, is the throw in GetLastAddedID necessary, should I use throw instead of throw ex, ...?

2 Answers 2

2

The exception handling in GetLastAddedIDHelper and GetLastAddedID do not serve any useful purpose in this case. Your intent, it would seem, is to simply catch the exception and then do nothing with it except to rethrow it. If that is the case then why bother catching it all? Just let the exception propagate up the stack until an exception handler that can handle it gets it, in the Button_Click_Action method in this case.

In this example your exception handling is actually harmful because throw ex will mess up your stack trace. You want to use throw instead.

Sign up to request clarification or add additional context in comments.

2 Comments

So basically, if the methods GetLastAddedIDHelper and GetLastAddedID are only used in other methods that will have exception handling it's better to just leave the try...catch if throwing is the only thing I do?
Yep, that is the short of it. And if you do find yourself in a situation where you need to rethrow (you probably will not being doing this very often but there are some use cases) then use throw; to rethrow properly. You can search for 'C# rethrow' on Google for more details on why.
0

You probably want to do throw; because throw ex; will reset the stack trace.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.