Empty catch

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DaTurk

    #1

    Empty catch

    What's the CLI equivalent of the empty catch?

    i.e.
    try{}
    catch
    {}

  • William DePalo [MVP VC++]

    #2
    Re: Empty catch

    "DaTurk" <mmagdits@hotma il.comwrote in message
    news:1179770123 .946621.121980@ y2g2000prf.goog legroups.com...
    What's the CLI equivalent of the empty catch?
    >
    i.e.
    try{}
    catch
    {}
    How about this:

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

    catch ( Exception^ )
    {
    }

    Regards,
    Will


    Comment

    • =?Utf-8?B?RGF2aWQgQW50b24=?=

      #3
      RE: Empty catch

      try
      {
      }
      catch(...)
      {
      }

      --
      David Anton
      Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters

      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


      "DaTurk" wrote:
      What's the CLI equivalent of the empty catch?
      >
      i.e.
      try{}
      catch
      {}
      >
      >

      Comment

      • SvenC

        #4
        Re: Empty catch

        DaTurk wrote:
        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

        Comment

        • Carl Daniel [VC++ MVP]

          #5
          Re: Empty catch

          "SvenC" <SvenC@communit y.nospamwrote in message
          news:e4DEQODnHH A.3736@TK2MSFTN GP03.phx.gbl...
          DaTurk wrote:
          >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(...) ?
          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


          Comment

          • DaTurk

            #6
            Re: Empty catch

            On May 22, 12:39 am, "SvenC" <S...@community .nospamwrote:
            DaTurk wrote:
            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
            My apologies, I was refering to c#

            Comment

            • Jochen Kalmbach [MVP]

              #7
              Re: Empty catch

              Hi Carl!
              >>>What's the CLI equivalent of the empty catch?
              >>>
              >>>i.e.
              >>>try{}
              >>>catch
              >>>{}
              >
              It's allowed in C#. It's equivalent to the C++/CLI construct:
              >
              try
              {
              }
              catch(Exception ^)
              {
              }
              No. "catch(Exceptio n^)" is *not* the same as "catch" in C#!

              "catch(Exceptio n^)" will *only* catch exceptions which are derived from
              "System.Excepti on"!

              "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/lib...b6(VS.80).aspx


              --
              Greetings
              Jochen

              My blog about Win32 and .NET

              Comment

              • Jochen Kalmbach [MVP]

                #8
                Re: Empty catch

                DaTurk schrieb:
                What's the CLI equivalent of the empty catch?
                >
                i.e.
                try{}
                catch
                {}
                try {}
                catch(System::O bject^)
                {}

                --
                Greetings
                Jochen

                My blog about Win32 and .NET

                Comment

                • Carl Daniel [VC++ MVP]

                  #9
                  Re: Empty catch

                  "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach @holzma.dewrote in message
                  news:%23L8hGRLn HHA.4848@TK2MSF TNGP05.phx.gbl. ..
                  Hi Carl!
                  >
                  >>>>What's the CLI equivalent of the empty catch?
                  >>>>
                  >>>>i.e.
                  >>>>try{}
                  >>>>catch
                  >>>>{}
                  >>
                  >It's allowed in C#. It's equivalent to the C++/CLI construct:
                  >>
                  >try
                  >{
                  >}
                  >catch(Exceptio n^)
                  >{
                  >}
                  >
                  No. "catch(Exceptio n^)" is *not* the same as "catch" in C#!
                  >
                  "catch(Exceptio n^)" will *only* catch exceptions which are derived from
                  "System.Excepti on"!
                  >
                  "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.Exceptio n!

                  -cd


                  Comment

                  • Ben Voigt

                    #10
                    Re: Empty catch

                    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?


                    Comment

                    • Jochen Kalmbach [MVP]

                      #11
                      Re: Empty catch

                      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.Excepti on". But you can throw
                      *any* object as an exception. Therefor, if you want to catch *all*
                      exceptions, you need to catch "System.Obj ect" instead of "System.Excepti on"!

                      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::O bject ^o)
                      {
                      System::Console ::WriteLine("Te st1: " + o->GetType()->ToString());
                      }

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

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


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

                      Greetings
                      Jochen

                      Comment

                      • Ben Voigt

                        #12
                        Re: Empty catch


                        "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach @holzma.dewrote in message
                        news:enBVzKQnHH A.4628@TK2MSFTN GP06.phx.gbl...
                        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.Excepti on". But you can throw
                        *any* object as an exception. Therefor, if you want to catch *all*
                        exceptions, you need to catch "System.Obj ect" instead of
                        "System.Excepti on"!
                        >
                        See:
                        I wasn't doubting that catching System.Object will catch all exceptions...
                        but wouldn't catching System.Exceptio n also catch all exceptions, decoding
                        managed objects derived from Exception, and wrapping everything else in
                        SEHException?
                        >
                        #pragma unmanaged
                        >
                        void Test1()
                        {
                        throw 1;
                        }
                        >
                        #pragma managed
                        >
                        void Test2()
                        {
                        throw 2;
                        }
                        >
                        void Test3()
                        {
                        throw gcnew System::Object( );
                        }
                        >
                        int main()
                        {
                        try
                        {
                        Test1();
                        }
                        catch(System::O bject ^o)
                        {
                        System::Console ::WriteLine("Te st1: " + o->GetType()->ToString());
                        }
                        >
                        try
                        {
                        Test2();
                        }
                        catch(System::O bject ^o)
                        {
                        System::Console ::WriteLine("Te st2: " + o->GetType()->ToString());
                        }
                        >
                        try
                        {
                        Test3();
                        }
                        catch(System::O bject ^o)
                        {
                        System::Console ::WriteLine("Te st3: " + o->GetType()->ToString());
                        }
                        }
                        >
                        >
                        Output is:
                        Test1: System.Runtime. InteropServices .SEHException
                        Test2: System.Runtime. InteropServices .SEHException
                        Test3: System.Object
                        >
                        Greetings
                        Jochen

                        Comment

                        • Jochen Kalmbach [MVP]

                          #13
                          Re: Empty catch

                          Hi Ben!
                          I wasn't doubting that catching System.Object will catch all exceptions...
                          but wouldn't catching System.Exceptio n 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.Excepti on" will catch *all* exceptions... regardless of
                          SEHException...

                          Greetings
                          Jochen

                          Comment

                          • Mattias Sjögren

                            #14
                            Re: Empty catch

                            >I wasn't doubting that catching System.Object will catch all exceptions...
                            >but wouldn't catching System.Exceptio n 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.Except ion" will catch *all* exceptions... regardless of
                            >SEHException.. .

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


                            Mattias

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

                            Comment

                            Working...