# \[pitch\] Printing versions of try! and try?

**URL:** <https://forums.swift.org/t/pitch-printing-versions-of-try-and-try/1257>\
**Category:** Pitches\
**Created:** [January 31, 2016, 5:41am UTC](https://forums.swift.org/t/pitch-printing-versions-of-try-and-try/1257 "2016-01-31T05:41:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [January 31, 2016, 5:41am UTC](https://forums.swift.org/t/pitch-printing-versions-of-try-and-try/1257/1 "2016-01-31T05:41:38Z")

</div>

Not sure where to post this, so starting here. It's a behavior change more than an API change. I'd like to request that try! and try? print errors in debug builds and in the playground. Right now, I use the following work-around.

/// Replacement for `try?` that introduces printing for  
/// error conditions instead of discarding those errors  
///  
/// - Parameter shouldCrash: defaults to false. When set to true  
/// will raise a fatal error, emulating try! instead of try?  
///  
/// ```swift  
/// attempt {  
/// let mgr = NSFileManager.defaultManager()  
/// try mgr.createDirectoryAtPath(  
/// "/Users/notarealuser",  
/// withIntermediateDirectories: true,  
/// attributes: nil)  
/// }  
/// ```  
///  
public func attempt\<T\>(  
&nbsp;&nbsp;&nbsp;&nbsp;line line: Int = \_\_LINE\_\_,  
&nbsp;&nbsp;&nbsp;&nbsp;shouldCrash: Bool = false,  
&nbsp;&nbsp;&nbsp;&nbsp;closure: () throws -\> T  
&nbsp;&nbsp;&nbsp;&nbsp;) -\> T?  
{  
&nbsp;&nbsp;&nbsp;&nbsp;do {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/// Return executes only if closure succeeds  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try closure()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;} catch {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/// Emulate try! by crashing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if shouldCrash {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Fatal error on line \(line): \(error)")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fatalError()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/// Force print and return nil like try?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("Error \(line): \(error)")  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return nil  
&nbsp;&nbsp;&nbsp;&nbsp;}  
}
