1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "base/mac/scoped_nsexception_enabler.h"
7 #import "base/lazy_instance.h"
8 #import "base/threading/thread_local.h"
10 // To make the |g_exceptionsAllowed| declaration readable.
11 using base::LazyInstance;
12 using base::ThreadLocalBoolean;
14 // When C++ exceptions are disabled, the C++ library defines |try| and
15 // |catch| so as to allow exception-expecting C++ code to build properly when
16 // language support for exceptions is not present. These macros interfere
17 // with the use of |@try| and |@catch| in Objective-C files such as this one.
18 // Undefine these macros here, after everything has been #included, since
19 // there will be no C++ uses and only Objective-C uses from this point on.
25 // Whether to allow NSExceptions to be raised on the current thread.
26 LazyInstance<ThreadLocalBoolean>::Leaky
27 g_exceptionsAllowed = LAZY_INSTANCE_INITIALIZER;
34 bool GetNSExceptionsAllowed() {
35 return g_exceptionsAllowed.Get().Get();
38 void SetNSExceptionsAllowed(bool allowed) {
39 return g_exceptionsAllowed.Get().Set(allowed);
42 id RunBlockIgnoringExceptions(BlockReturningId block) {
45 base::mac::ScopedNSExceptionEnabler enable;
48 @catch(id exception) {
53 ScopedNSExceptionEnabler::ScopedNSExceptionEnabler() {
54 was_enabled_ = GetNSExceptionsAllowed();
55 SetNSExceptionsAllowed(true);
58 ScopedNSExceptionEnabler::~ScopedNSExceptionEnabler() {
59 SetNSExceptionsAllowed(was_enabled_);