Added support for Objective-C exceptions

This commit is contained in:
Phil Nash 2012-05-21 21:51:16 +01:00
parent 371db8b42f
commit 35b31fa78a
3 changed files with 40 additions and 0 deletions

View File

@ -10,6 +10,10 @@
#include "catch_interfaces_exception.h" #include "catch_interfaces_exception.h"
#ifdef __OBJC__
#import "Foundation/Foundation.h"
#endif
namespace Catch { namespace Catch {
class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
@ -24,8 +28,18 @@ namespace Catch {
virtual std::string translateActiveException() const { virtual std::string translateActiveException() const {
try { try {
#ifdef __OBJC__
// In Objective-C try objective-c exceptions first
@try {
throw; throw;
} }
@catch (NSException *exception) {
return toString( [exception description] );
}
#else
throw;
#endif
}
catch( std::exception& ex ) { catch( std::exception& ex ) {
return ex.what(); return ex.what();
} }

View File

@ -41,6 +41,18 @@ OC_TEST_CASE( "OCTest/test2", "This is another test case" )
REQUIRE( obj.int_val == 2 ); REQUIRE( obj.int_val == 2 );
} }
OC_TEST_CASE( "OCTest/throws/objc", "throws an Objective-C exception" )
{
@throw [[NSException alloc] initWithName: NSGenericException
reason: @"Objective-C exception"
userInfo: nil];
}
OC_TEST_CASE( "OCTest/throws/stdc++", "throws a std c++ exception" )
{
throw std::domain_error( "std C++ exception" );
}
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
template<typename T> template<typename T>
void useObject( const T& object ){} void useObject( const T& object ){}

View File

@ -3157,6 +3157,10 @@ namespace Catch {
// #included from: catch_exception_translator_registry.hpp // #included from: catch_exception_translator_registry.hpp
#ifdef __OBJC__
#import "Foundation/Foundation.h"
#endif
namespace Catch { namespace Catch {
class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry { class ExceptionTranslatorRegistry : public IExceptionTranslatorRegistry {
@ -3171,8 +3175,18 @@ namespace Catch {
virtual std::string translateActiveException() const { virtual std::string translateActiveException() const {
try { try {
#ifdef __OBJC__
// In Objective-C try objective-c exceptions first
@try {
throw; throw;
} }
@catch (NSException *exception) {
return toString( [exception description] );
}
#else
throw;
#endif
}
catch( std::exception& ex ) { catch( std::exception& ex ) {
return ex.what(); return ex.what();
} }