Documentation

    Table of Content
    • CodePorting.Translator Product Family
      • CodePorting.Translator Cs2Cpp
        • Getting Started
          • Product Overview
          • Installation
          • Licensing
          • How to use CodePorting.Translator Cs2Cpp
            • How to Use Command line to translate and build Projects
            • How to Use GUI to translate and build Projects
        • Developer Guide
          • Translating Simple C# Projects
            • Translating Simple Console Application
            • Translating Simple Library
            • Translating Simple NUnit Test
          • Translating Dependent C# Projects
            • Translating Dependent Console Application
            • Translating Dependent Library
            • Translating Dependent NUnit Test
          • Qt support
          • Translating Complex C# Projects
            • How to Convert Complex C# Console Application to C++
            • How to Convert Complex C# Library to C++
            • How to Convert Complex C# NUnit Test to C++
          • What Converts to What
            • AbstractClasses
            • BreakStatements
            • ClassAutoProperties
            • ClassConstructors
            • ClassEvents
            • ClassFinalizers
            • ClassGenericMethods
            • ClassIndexers
            • ClassMethods
            • ClassProperties
            • ClassStaticConstructor
            • ClassStaticMethods
            • ClassStaticProperties
            • ClassVirtualMethods
            • ContinueStatements
            • Delegates
            • DoWhileStatements
            • Enums
            • EnumTypeCast
            • Exceptions
            • ExpectedException
            • ForeachStatements
            • ForeachStatementsStd
            • ForStatements
            • GenericClasses
            • GenericDelegates
            • GenericInterfaces
            • GenericStructs
            • IfStatements
            • LambdaExpressions
            • NestedClasses
            • ReturnStatements
            • SimpleClass
            • SimpleInterface
            • SimpleStruct
            • SimpleTest
            • StandardTypeCast
            • StaticClass
            • SwitchStatements
            • TestWithSetupMethods
            • ThrowStatements
            • TryCatchFinallyStatements
            • TryCatchStatements
            • TryFinallyStatements
            • VarExpressions
            • WhileStatements
          • CodePorting Translator Cs2Cpp Attributes
          • CodePorting Translator Cs2Cpp Configuration File
            • CodePorting.Translator Cs2Cpp Configuration File Structure
            • Attributes in Configuration file
            • Configuration file Nodes
            • Configuration file Options
          • Memory Management Model
            • Memory Management Model Description
            • Using aliasing constructor to create a smart pointer
          • Cmake Support
          • C++ code injection
          • C++ user-defined exception classes
          • Limitations and Bugs
            • Translator Limitations and Bugs
            • Library Limitations and Bugs
            • Cpp Code Injection
        • Release Notes
          • 2022
            • CodePorting.Translator Cs2Cpp 22.6
            • CodePorting.Native Cs2Cpp 22.5
            • CodePorting.Native Cs2Cpp 22.4
            • CodePorting.Native Cs2Cpp 22.3
            • CodePorting.Native Cs2Cpp 22.2
            • CodePorting.Native Cs2Cpp 22.1
          • 2021
            • CodePorting.Native Cs2Cpp 21.12
            • CodePorting.Native Cs2Cpp 21.11
            • CodePorting.Native Cs2Cpp 21.10.1
            • CodePorting.Native Cs2Cpp 21.10
            • CodePorting.Native Cs2Cpp 21.9
            • CodePorting.Native Cs2Cpp 21.8
            • CodePorting.Native Cs2Cpp 21.7
            • CodePorting.Native Cs2Cpp 21.6
            • CodePorting.Native Cs2Cpp 21.5
            • CodePorting.Native Cs2Cpp 21.4
            • CodePorting.Native Cs2Cpp 21.3
            • CodePorting.Native Cs2Cpp 21.2
            • CodePorting.Native Cs2Cpp 21.1
          • 2020
            • CodePorting.Native Cs2Cpp 20.12
            • CodePorting.Native Cs2Cpp 20.11
            • CodePorting.Native Cs2Cpp 20.10
            • CodePorting.Native Cs2Cpp 20.9
            • CodePorting.Native Cs2Cpp 20.8
            • CodePorting.Native Cs2Cpp 20.7
            • CodePorting.Native Cs2Cpp 20.6
            • CodePorting.Native Cs2Cpp 20.5
            • CodePorting.Native Cs2Cpp 20.4
            • CodePorting.Native Cs2Cpp 20.3
            • CodePorting.Native Cs2Cpp 20.2
            • CodePorting.Native Cs2Cpp 20.1
          • 2019
            • CodePorting.Native Cs2Cpp 19.1
            • CodePorting.Native Cs2Cpp 19.2
            • CodePorting.Native Cs2Cpp 19.3
            • CodePorting.Native Cs2Cpp 19.4
            • CodePorting.Native Cs2Cpp 19.4.1
            • CodePorting.Native Cs2Cpp 19.5
            • CodePorting.Native Cs2Cpp 19.6
            • CodePorting.Native Cs2Cpp 19.7
            • CodePorting.Native Cs2Cpp 19.8
            • CodePorting.Native Cs2Cpp 19.9
            • CodePorting.Native Cs2Cpp 19.10
            • CodePorting.Native Cs2Cpp 19.11
            • CodePorting.Native Cs2Cpp 19.12
          • 2018
            • CodePorting.Native Cs2Cpp 18.9
            • CodePorting.Native Cs2Cpp 18.9.1
            • CodePorting.Native Cs2Cpp 18.10
            • CodePorting.Native Cs2Cpp 18.11
            • CodePorting.Native Cs2Cpp 18.12
    1. Home
    2. CodePorting.Translator Product Family
    3. CodePorting.Translator Cs2Cpp
    4. Developer Guide
    5. What Converts to What
    6. ThrowStatements

    ThrowStatements

    What's on this Page

      • Source C# Code
      • Translated Code
        • C++ Header
        • C++ Source Code

    This example demonstrates how throw statement is translated to C++.

    Additional command-line options passed to CodePorting.Translator.Cs2Cpp: none.

    Source C# Code

    using System;
    
    namespace StatementsPorting
    {
        public class SomeException : Exception
        {
        }
    
        public class OtherException : Exception
        {
        }
    
        public class ThrowStatements
        {
            public void Throw(int value)
            {
                if (value < 10)
                {
                    Console.WriteLine("Too small value");
                    throw new SomeException();
                }
                if (value > 20)
                {
                    Console.WriteLine("Too big value");
                    throw new OtherException();
                }
            }
    
            public void RethrowSame()
            {
                try
                {
                    InnerMethod();
                }
                catch (SomeException)
                {
                    Console.WriteLine("Catch SomeException");
                    throw;
                }
            }
    
            public void RethrowOther()
            {
                try
                {
                    InnerMethod();
                }
                catch (SomeException)
                {
                    Console.WriteLine("Catch SomeException");
                    throw new OtherException();
                }
            }
    
            private void InnerMethod()
            {
            }
        }
    }

    Translated Code

    C++ Header

    #pragma once
    
    #include <system/object.h>
    #include <system/exceptions.h>
    #include <cstdint>
    
    namespace StatementsPorting {
    
    class Details_SomeException;
    using SomeException = System::ExceptionWrapper<Details_SomeException>;
    
    class Details_SomeException : public System::Details_Exception
    {
        typedef Details_SomeException ThisType;
        typedef System::Details_Exception BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_DECL();
        
        friend class System::ExceptionWrapperHelper;
        template <typename T> friend class System::ExceptionWrapper;
        
    protected:
    
        [[noreturn]] void DoThrow(const System::ExceptionPtr& self) const override;
        
        Details_SomeException();
        
        MEMBER_FUNCTION_MAKE_OBJECT_DECLARATION(Details_SomeException, CODEPORTING_ARGS());
        
    };
    
    class Details_OtherException;
    using OtherException = System::ExceptionWrapper<Details_OtherException>;
    
    class Details_OtherException : public System::Details_Exception
    {
        typedef Details_OtherException ThisType;
        typedef System::Details_Exception BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_DECL();
        
        friend class System::ExceptionWrapperHelper;
        template <typename T> friend class System::ExceptionWrapper;
        
    protected:
    
        [[noreturn]] void DoThrow(const System::ExceptionPtr& self) const override;
        
        Details_OtherException();
        
        MEMBER_FUNCTION_MAKE_OBJECT_DECLARATION(Details_OtherException, CODEPORTING_ARGS());
        
    };
    
    class ThrowStatements : public System::Object
    {
        typedef ThrowStatements ThisType;
        typedef System::Object BaseType;
        
        typedef ::System::BaseTypesInfo<BaseType> ThisTypeBaseTypesInfo;
        RTTI_INFO_DECL();
        
    public:
    
        void Throw(int32_t value);
        void RethrowSame();
        void RethrowOther();
        
    private:
    
        void InnerMethod();
        
    };
    
    } // namespace StatementsPorting
    

    C++ Source Code

    #include "ThrowStatements.h"
    
    #include <system/string.h>
    #include <system/console.h>
    
    namespace StatementsPorting {
    
    RTTI_INFO_IMPL_HASH_NAMED(927407390u, ::StatementsPorting::Details_SomeException, "StatementsPorting::SomeException", ThisTypeBaseTypesInfo);
    
    [[noreturn]] void Details_SomeException::DoThrow(const System::ExceptionPtr& self) const
    {
        throw System::ExceptionWrapper<Details_SomeException>(self);
    }
    
    Details_SomeException::Details_SomeException() : System::Details_Exception()
    {
    }
    
    MEMBER_FUNCTION_MAKE_OBJECT_DEFINITION(Details_SomeException, CODEPORTING_ARGS(), CODEPORTING_ARGS());
    
    RTTI_INFO_IMPL_HASH_NAMED(1983333724u, ::StatementsPorting::Details_OtherException, "StatementsPorting::OtherException", ThisTypeBaseTypesInfo);
    
    [[noreturn]] void Details_OtherException::DoThrow(const System::ExceptionPtr& self) const
    {
        throw System::ExceptionWrapper<Details_OtherException>(self);
    }
    
    Details_OtherException::Details_OtherException() : System::Details_Exception()
    {
    }
    
    MEMBER_FUNCTION_MAKE_OBJECT_DEFINITION(Details_OtherException, CODEPORTING_ARGS(), CODEPORTING_ARGS());
    
    RTTI_INFO_IMPL_HASH(922865773u, ::StatementsPorting::ThrowStatements, ThisTypeBaseTypesInfo);
    
    void ThrowStatements::Throw(int32_t value)
    {
        if (value < 10)
        {
            System::Console::WriteLine(u"Too small value");
            throw SomeException();
        }
        if (value > 20)
        {
            System::Console::WriteLine(u"Too big value");
            throw OtherException();
        }
    }
    
    void ThrowStatements::RethrowSame()
    {
        try
        {
            InnerMethod();
        }
        catch (SomeException& )
        {
            System::Console::WriteLine(u"Catch SomeException");
            throw;
        }
        
    }
    
    void ThrowStatements::RethrowOther()
    {
        try
        {
            InnerMethod();
        }
        catch (SomeException& )
        {
            System::Console::WriteLine(u"Catch SomeException");
            throw OtherException();
        }
        
    }
    
    void ThrowStatements::InnerMethod()
    {
    }
    
    } // namespace StatementsPorting