O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

13.5. Catching generic exceptions

It may seem that a catch block specified with the System.Exception type (i.e. catch(System.Exception)) can handle any type of exception object thrown, since all exception classes are subclasses of System.Exception. This is true only if the exception is thrown from C# code.

Since C# may work with other .NET languages (or even non- .NET languages, such as a COM object written in VB 6 or VC++ 6), it is possible that these codes might throw their own exception objects. And since they may not be .NET-compliant, it is possible that these exception objects are not subclasses of System.Exception. Under such circumstances, you can omit the exception type altogether after the catch keyword. Examine line 16 in this code ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required