-1

I have a C# application..I continuously get a null reference exception..I manage to catch this exception and log it..But i doubt if this exception will affect the performance of my application..Please note that i am not trying to avoid the exception instead i need to know if this exception affects the performance of my application if it is continuously fired .

Techy
  • 315
  • 1
  • 5
  • 14

4 Answers4

4

If you're getting a NullReferenceException, you should fix it rather than catching it. You should only ever catch it if it occurs in code in a way that you cannot fix (e.g. a broken third party library). A NullReferenceException always indicates a programming error somewhere.

As for performance - it depends on what you mean by "continuously". Exceptions can be horribly expensive if they're thrown when nothing's really wrong - they're fine when used properly. How many are you seeing per second, for example? Note that when running in a debugger, exceptions are often much more expensive than they would be when a debugger isn't attached.

As ever, when you're worried about performance, you should test the performance, so you can use hard data to make decisions.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

It depends where you handle the exceptions and how often they happen. There is a good article on CodeProject regarding exceptions and performance, I suggest you read it.

dodsky
  • 504
  • 4
  • 11
0

Yes it affects it depend upon the frequency of it .

For further info please refer Link and it may also help you Link

Community
  • 1
  • 1
Sunny
  • 3,185
  • 8
  • 34
  • 66
0

Exceptions have been designed to alter performances to a minimum when they are not thrown : adding a try/catch block should have very limited impact to your application performances. Therefore, I'd recommand to add as many try/catch blocks as required to catch any exception IN THE RIGHT LEVEL.

However, throwing and catching an exception may be very expensive. Application has to switch contexts, dispose any element in using blocks... And that's why I agree with @Ramhound : you ought to fix the exception rather than catching it.

Kek
  • 3,145
  • 2
  • 20
  • 26