Code::Blocks Forums

Developer forums (C::B DEVELOPMENT STRICTLY!) => Plugins development => Topic started by: threeS on December 14, 2014, 04:21:00 pm

Title: Plugin Error Catching (DB Connection)
Post by: threeS on December 14, 2014, 04:21:00 pm
Good day to all!

I have these line in my developed plugin for codeblocks IDE, the connection for my postgresql database.
Quote
< . . . codes . . . >
     conn = PQconnectdb("dbname=dbname user=postgres password=postgres host=127.0.0.1 port=5432");
< . . . codes . . . >

I want to create a somewhat catch C++ code that catch if the connection fail and pops-up the fail error and not close the IDE or not crash it.

Can anybody help me please?  :(
God Bless and Merry Christmas to All!
Title: Re: Plugin Error Catching (DB Connection)
Post by: ollydbg on December 14, 2014, 04:27:23 pm
I want to create a somewhat catch C++ code that catch if the connection fail and pops-up the fail error and not close the IDE or not crash it.
You can simply use C++ try catch statement in your plugin source.
Code
your_plugin_event_handler_function()
{
  try
  {
    connect to your database
  }
  catch (error)
  {
    show a message box
  }
}
Title: Re: Plugin Error Catching (DB Connection)
Post by: threeS on December 14, 2014, 04:39:12 pm
Thank you for that,.
Yes i can create like,
Quote
catch (error)
  {
    show a message box
  }
}
because id tried something like this
Quote
if(PQstatus(conn) != CONNECTION_OK) {
        Manager::Get()->GetLogManager()->Log(_("Database connection failed!"));
}
it print the error in the IDE console and crash the IDE with an error something like "Aborted (core dumped)".

I want in my "show a message box" a pop-up like error message and will not crash my IDE.,

Is that possible guys?  :(
please help me :)
Title: Re: Plugin Error Catching (DB Connection)
Post by: oBFusCATed on December 14, 2014, 05:33:58 pm
For sure it is possible.
You have to read the documentation for the API you're trying to use. Learn how to detect and handle errors, change your code.
If there are crashes use a debugger to try to understand why.

Most of the times it is dereferencing null/invalid pointers.

p.s. This has nothing to do with C::B, it is just a generic programming problem.
Title: Re: Plugin Error Catching (DB Connection)
Post by: threeS on December 15, 2014, 06:24:13 pm
Yeah, i got you point sir.
I can handle the error catching, but what i'm asking is how to display the error like a pop-up on codeblocks IDE.
So, maybe a plugin warning pop-up related something?  :(
Title: Re: Plugin Error Catching (DB Connection)
Post by: oBFusCATed on December 15, 2014, 09:17:31 pm
Either use the cbMessageBox function, the InfoWindow class or the LogManager.
It is up to you to decide what you should do with your plugin.
Title: Re: Plugin Error Catching (DB Connection)
Post by: threeS on December 17, 2014, 05:35:13 am
thank you so much,.
i will give a shot for that  :)
Title: Re: Plugin Error Catching (DB Connection)
Post by: threeS on December 17, 2014, 06:57:04 pm
Additional guys.,
How to catch errors inside a plugin that prevent the IDE to crash?  :(
Title: Re: Plugin Error Catching (DB Connection)
Post by: oBFusCATed on December 17, 2014, 11:27:00 pm
Use a debugger to find the cause, then fix the bug.

If you read the c++ specification you'll find that you cannot catch segmentation faults/access violations as exceptions!
Title: Re: Plugin Error Catching (DB Connection)
Post by: threeS on December 18, 2014, 07:31:20 am
that is why . :(

I have another idea, maybe i can disable the plugin, so that the IDE will not crash.
How can it be possible?