Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2007

Tip: Looking for answers? Try searching our database.

Empty catch

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DaTurk - 21 May 2007 18:55 GMT
What's the CLI equivalent of the empty catch?

i.e.
try{}
catch
{}
William DePalo [MVP VC++] - 21 May 2007 19:26 GMT
> What's the CLI equivalent of the empty catch?
>
> i.e.
> try{}
> catch
> {}

How about this:

  try
  {
    Console::WriteLine(L"Hello World");
  }

  catch ( Exception^ )
  {
  }

Regards,
Will
David Anton - 22 May 2007 01:54 GMT
try
{
}
catch(...)
{
}

Signature

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
C++ to C# Converter: converts C++ to C#
Instant C++: converts C# or VB to C++/CLI

> What's the CLI equivalent of the empty catch?
>
> i.e.
> try{}
> catch
> {}
SvenC - 22 May 2007 06:39 GMT
> What's the CLI equivalent of the empty catch?
>
> i.e.
> try{}
> catch
> {}

Where is that empty catch allowed? It is not standard C++. Do you mean
catch(...) ?

--
SvenC
Carl Daniel [VC++ MVP] - 22 May 2007 17:53 GMT
>> What's the CLI equivalent of the empty catch?
>>
[quoted text clipped - 5 lines]
> Where is that empty catch allowed? It is not standard C++. Do you mean
> catch(...) ?

It's allowed in C#.  It's equivalent to the C++/CLI construct:

try
{
}
catch(Exception^)
{
}

Using catch(...) will also catch native exceptions, and possibly structured
(platform) exceptions, depending on the compiler options used.

-cd
Jochen Kalmbach [MVP] - 22 May 2007 22:00 GMT
Hi Carl!

>>>What's the CLI equivalent of the empty catch?
>>>
[quoted text clipped - 11 lines]
> {
> }

No. "catch(Exception^)" is *not* the same as "catch" in C#!

"catch(Exception^)" will *only* catch exceptions which are derived from
"System.Exception"!

"catch" will catch *all* Exceptions (derived from "System.Object")!
It also will catch native exceptins in C#!!!

Therefor the C++/CLI equivalent of "catch" is "catch(System::Object^)" !

See also: Basic Concepts in Using Managed Exceptions
http://msdn2.microsoft.com/en-us/library/df24ysb6(VS.80).aspx

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/

Carl Daniel [VC++ MVP] - 22 May 2007 22:05 GMT
> Hi Carl!
>
[quoted text clipped - 21 lines]
> "catch" will catch *all* Exceptions (derived from "System.Object")!
> It also will catch native exceptins in C#!!!

Egads!

I'd forgotten that the CLR actually supporting throwing something not
derived from System.Exception!

-cd
Ben Voigt - 22 May 2007 23:40 GMT
> It also will catch native exceptins in C#!!!

Wouldn't catch (Exception^) do so as well, giving an SEHException?  Or is
that only for p/invoke?
Jochen Kalmbach [MVP] - 23 May 2007 07:23 GMT
Hi Ben!
>> It also will catch native exceptins in C#!!!
>
> Wouldn't catch (Exception^) do so as well, giving an SEHException?  Or is
> that only for p/invoke?

SEHException is also derived from "System.Exception". But you can throw
*any* object as an exception. Therefor, if you want to catch *all*
exceptions, you need to catch "System.Object" instead of "System.Exception"!

See:

#pragma unmanaged

void Test1()
{
  throw 1;
}

#pragma managed

void Test2()
{
  throw 2;
}

void Test3()
{
  throw gcnew System::Object();
}

int main()
{
  try
  {
    Test1();
  }
  catch(System::Object ^o)
  {
    System::Console::WriteLine("Test1: " + o->GetType()->ToString());
  }

  try
  {
    Test2();
  }
  catch(System::Object ^o)
  {
    System::Console::WriteLine("Test2: " + o->GetType()->ToString());
  }

  try
  {
    Test3();
  }
  catch(System::Object ^o)
  {
    System::Console::WriteLine("Test3: " + o->GetType()->ToString());
  }
}

Output is:
Test1: System.Runtime.InteropServices.SEHException
Test2: System.Runtime.InteropServices.SEHException
Test3: System.Object

Greetings
  Jochen
Ben Voigt - 23 May 2007 14:19 GMT
> Hi Ben!
>>> It also will catch native exceptins in C#!!!
[quoted text clipped - 8 lines]
>
> See:

I wasn't doubting that catching System.Object will catch all exceptions...
but wouldn't catching System.Exception also catch all exceptions, decoding
managed objects derived from Exception, and wrapping everything else in
SEHException?

> #pragma unmanaged
>
[quoted text clipped - 52 lines]
> Greetings
>   Jochen
Jochen Kalmbach [MVP] - 23 May 2007 14:33 GMT
Hi Ben!

> I wasn't doubting that catching System.Object will catch all exceptions...
> but wouldn't catching System.Exception also catch all exceptions, decoding
> managed objects derived from Exception, and wrapping everything else in
> SEHException?

I don't understand the background of this question?

I don't know the internals of every assembly; so I don't know if
"System.Exception" will catch *all* exceptions... regardless of
SEHException...

Greetings
  Jochen
Mattias Sjögren - 23 May 2007 19:37 GMT
>> I wasn't doubting that catching System.Object will catch all exceptions...
>> but wouldn't catching System.Exception also catch all exceptions, decoding
[quoted text clipped - 6 lines]
>"System.Exception" will catch *all* exceptions... regardless of
>SEHException...

You can also use the RuntimeCompatibilityAttribute on your assembly
with WrapNonExceptionThrows = true to make sure all exceptions you see
are derived from Exception (if they aren't already, they get wrapped
in a RuntimeWrappedException). That's what C# does for example.

Mattias

Signature

Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

DaTurk - 22 May 2007 19:50 GMT
> > What's the CLI equivalent of the empty catch?
>
[quoted text clipped - 8 lines]
> --
> SvenC

My apologies, I was refering to c#
Jochen Kalmbach [MVP] - 22 May 2007 22:01 GMT
DaTurk schrieb:
> What's the CLI equivalent of the empty catch?
>
> i.e.
> try{}
> catch
> {}

try {}
catch(System::Object^)
{}

Signature

Greetings
  Jochen

   My blog about Win32 and .NET
   http://blog.kalmbachnet.de/


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.