Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Cast Exception

Stumbled on some old code that is throwing and empty catching some cast exceptions (about 20 per trip :( )

What if any is the performance hit were taking due to this? Should I be worried about this or is the overhead simply in the try / catch

Surprisingly lacking information on the topic of exception performance with C#.

Thanks, from yours truly.

like image 323
Jake Kalstad Avatar asked Jan 03 '11 17:01

Jake Kalstad


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

The exceptions are going to slow you down more than most average lines of code. Instead of casting and then catching the exception, do a check instead. For example

BAD

myType foo = (myType)obj;
foo.ExecuteOperation();

GOOD

myType foo = obj as myType;
if (foo != null)
{
     foo.ExecuteOperation();
}
like image 115
GWLlosa Avatar answered Sep 28 '22 08:09

GWLlosa


That's bad for two reasons.

  1. Exceptions are slow, there is quite a performance hit. I don't think it'd take an entire millisecond as Matt pointed out, but they are slow enough that you want to avoid them in normal operation.
  2. Unless you have a good reason, you shouldn't catch empty exceptions. You're just hiding problems. Better that a program crashes than that it carries on with potentially dangerous bugs.

If they're just try { } finally { } groups, then it's all good -- there's no overhead there. However, try { } catch { } is both potentially dangerous and potentially slow.

As for documentation, this is pretty good: http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx#Don%27tuseexceptionhandlingasmeansofreturninginformationfromamethod18

Edit: just realized you said empty catching exceptions, not catching empty exceptions. Either way, unless you're dealing with IO, you probably want to avoid doing that for performance's sake.

like image 22
Rei Miyasaka Avatar answered Sep 28 '22 07:09

Rei Miyasaka