4

I have a very simple question. Would really appreciate if a C++ programmer can guide me. I want to write the C# code below in C++ dll. Can you please guide?

C# code to be translated:

void someMethod{
    try
    {
    //performs work, that can throw an exception
    }
    catch(Exception ex)
    {
        Log(ex.Message);//logs the message to a text file
    }
}

//can leave this part, i can implement it in C++
public void Log(string message)
{
//logs message in a file... 
}

I have already done something similar in C++ but I can't get the message part of try{}catch(...).

Wolf
  • 9,679
  • 7
  • 62
  • 108
InfoLearner
  • 14,952
  • 20
  • 76
  • 124

5 Answers5

2
void someMethod{
//performs work
try
{}
catch(std::exception& ex)
{
    //Log(ex.Message);//logs the message to a text file
    cout << ex.what(); 
}
catch(...)
{
    // Catch all uncaught exceptions 
}

But use exceptions with care. Exceptions in C++

Community
  • 1
  • 1
DumbCoder
  • 5,696
  • 3
  • 29
  • 40
  • +1 for the link to gotw. That article is a bit old and modern libraries have adapted to use exceptions properly, but they can still be quite tricky. – Fred Foo Mar 12 '11 at 13:42
  • Catching all is only correct, if the **function is exported** by the DLL. – Wolf Feb 20 '14 at 09:39
  • the performs work comment was corrected in the question – Wolf Feb 20 '14 at 09:44
2

You may probably want to catch all the exceptions thrown.
So add catch all (catch(…)) also for that:

try
{
   // ...
}
catch(const std::exception& ex)
{
   std::cout << ex.what() << std::endl;
}
catch(...)
{
   std::cout << "You have got an exception,"
                "better find out its type and add appropriate handler"
                "like for std::exception above to get the error message!" << std::endl;
}
Hovhannes Grigoryan
  • 1,151
  • 1
  • 8
  • 11
1

Try:

#include <exception.h>
#include <iostream>
void someMethod() {
    //performs work
    try {

    }
    catch(std::exception ex) {
        std::cout << ex.what() << std::endl;
    }
}
steveo225
  • 11,394
  • 16
  • 62
  • 114
  • This will potentially fail on more complex exceptions. Because you do not catch by reference you are initiating a copy construction of the exception into `ex`. If the exception is derived from std::exception (probably always true) you will slice the exception during the copy (weather this is an actual problem will depend on the actual exception). As a result always catch by reference and prefer const reference. – Martin York Mar 12 '11 at 13:10
  • There was supposed to be an `&` there, aparantly I cannot type. Also, it should probably be `const` unless there is some need to modify the exception. Additionally, `catch(...)` should be added (here or somewhere in the app) to prevent unhandled exceptions. – steveo225 Mar 12 '11 at 13:18
  • There's a simple remedy to typing errors: edit your answer. It's important for answers on SO to not be mistaken or misleading. – Jim Balter Mar 12 '11 at 13:25
  • If you don't notice the error, its hard to edit it, and once somebody else points it out, it is sort of moot to edit it after. Especially when you aren't the best answer any longer. – steveo225 Mar 12 '11 at 13:31
  • the performs work comment was corrected in the question – Wolf Feb 20 '14 at 09:44
1

The reason you can't get the exception with:

try
{
}
catch (...)
{
}

is because you aren't declaring the exception variable in the catch block. That would be the equivalent of (in C#):

try
{
}
catch
{
    Log(ex.Message); // ex isn't declared
}

You can get the exception with the following code:

try
{
}
catch (std::exception& ex)
{
}
Marlon
  • 19,924
  • 12
  • 70
  • 101
0

I assume that the requested function is exported by the DLL, so I prevent any flying exception.

#include <exception.h>

// some function exported by the DLL
void someFunction() 
{
    try {
        // perform the dangerous stuff
    } catch (const std::exception& ex) {
        logException(ex.what());
    } catch (...) {
        // Important don't return an exception to the caller, it may crash
        logException("unexpected exception caught");
    }
}

/// log the exception message
public void logException(const char* const msg)
{
    // write message in a file... 
}
Wolf
  • 9,679
  • 7
  • 62
  • 108