В данном предложении необходимо использовать глагол do в форме Past Continuous, так как действие происходило в определенный момент в прошлом.
I was doing carefully the exercise when the teacher came to my desk.
Ответ: I was doing carefully the exercise when the teacher came to my desk.