• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 706
  • Last Modified:

C++ throw exception

How can I write this to throw an exception when m_orderMatcher.find( securityID, side, id ) finds nothing?


 void Application::processCancel( const std::string& id,
                                  const std::string& securityID, Order::Side side )
 {
   Order & order = m_orderMatcher.find( securityID, side, id );
   order.cancel();
   cancelOrder( order );
   m_orderMatcher.erase( order );
}



   try
   {
      processCancel( origClOrdID, securityID, convert( side ) )
   }
   catch ( std::exception& ) {}
0
mcgilljd
Asked:
mcgilljd
  • 3
  • 2
1 Solution
 
jkrCommented:
That would not be a big deal - that is, if you could specify what 'find()' returns if no matcj is found. In general, that could be as simple as

#include <stdexcept>

 void Application::processCancel( const std::string& id,
                                  const std::string& securityID, Order::Side side )
 {
   Order & order = m_orderMatcher.find( securityID, side, id );

   if ( [nothing found] )
      throw std::exception("No match found");

   order.cancel();
   cancelOrder( order );
   m_orderMatcher.erase( order );
}

Open in new window

0
 
mcgilljdAuthor Commented:
Order& find( std::string securityID, Order::Side side, std::string id )
  {
    Markets::iterator i = m_markets.find( securityID );
    if ( i == m_markets.end() ) throw std::exception();
    return i->second.find( side, id );
  }
0
 
jkrCommented:
If 'find()' already throws an exception, there's nothing left for you to do - consider that angle already covered ;o)

You could catch and re-throw an exception, but that would be kinda pointless.
0
 
mcgilljdAuthor Commented:
Maybe I don't understand exceptions, the code after an exception is thrown still executes?
So maybe I do something like:


 void Application::onMessage( const FIX44::OrderCancelReplaceRequest& message)
{

 try
   {
    processCancel( origClOrdID, securityID, convert( side ) );
   }
   catch ( std::exception& ) { send a reject message(); return;}

More code that I don't want to execute if there is nothing found to cancel..

}
0
 
jkrCommented:
>>the code after an exception is thrown still executes?

No, it shouldn't - the control flow should be like

    if ( i == m_markets.end() ) throw std::exception(); // exception thrown...

[...]

 catch ( std::exception& ) { 

   // ... and you end up right here
   send a reject message(); return;

}

Open in new window

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now