10.5. Design Considerations

Programming with exceptions in C# introduces several design considerations because of performance trade-offs.

C# exceptions are first-class objects. Throwing exceptions involves creating new objects, and if these objects are not released, you can get memory leaks. Although C# and Java both alleviate this problem through automatic garbage collection, excessive creation of short-lived objects forces the garbage collector to run often and hampers runtime performance.

Another design consideration is that catching exceptions involves diverting the flow of execution, and that may slow your program. Therefore, you should use exception handling to deal with exceptional cases and not to control program flow (that is, in the ...

Get .NET for Java Developers: Migrating to C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.