Yes, it can be confusing as the two words have very similar meanings when used in this way. "While" usually means "during the time which (the doctor is treating the patient)". "When" can refer to a specific point in time, rather than a duration.
New words, one handy idiom, and a 2-minute quiz — delivered to your inbox to keep your streak alive.