<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">My particular situation was a conversion from Java. There are about 10 different add() methods with different parameters in its containing class. Only one of the add() overloads could cause a RuntimeException. In Java, we use a subclass of RuntimeException as the ReallyBadError indicator that is catchable near the top of the call stack, and possibly recoverable, such as by restarting a thread. The problem is that the calling code is spread across dozens of classes in several different products. Trying to figure out which of the add calls need to be enclosed with this:<div class=""><font face="Courier" class=""><b class=""> do {</b></font><div class=""><font face="Courier" class=""><b class=""> try add()</b></font></div><div class=""><font face="Courier" class=""><b class=""> }</b></font></div><div class=""><font face="Courier" class=""><b class=""> catch()...</b></font></div><div class="">is a giant hunt for needles in a haystack, and even if we could find them all would be a maintenance nightmare.</div><div class=""><br class=""></div><div class="">The fact that I bumped into this situation converting a bunch of code from Java is irrelevant. The programming language feature I desire (unchecked thingies that unwind the stack until running into a relevant try/catch) is simply absent in Swift.</div><div class=""><br class=""></div><div class="">Here is a more in depth description of why that feature is important. I need a non-checked <whateveryoucallit> for the *almost* fatal situations that are recoverable at a very high level. NullPointer is the prime example of a non-checked exception in Java. Letting the programmer write code that decides how to deal with such a situation is all I want in Swift. Unfortunately, assert() is irrelevant to the problem and fatalError() is not acceptable. The server needs to stay up, even in the face of latent bugs causing NullPointer exceptions and user-defined versions of similar errors.</div><div class=""><br class=""></div><div class="">The Swift requirement that all <b class=""><font face="Courier" class="">throw <struct></font></b> statenments must be declared with "throws" in the containing method s a real deal killer for big APIs that want to provide the option for the programmer to build some type of recovery mechanism.</div><div class=""><br class=""></div><div class="">Specifically, the <font color="#0056d6" class="">“Use this function to stop the program...</font>" approach you suggest is simply unacceptable. I can't live with the *entire* program to stopping. Think of a program such as Apache Server and you will understand what I'm talking about.</div><div class=""> </div><div class=""><br class=""></div><div class=""><div class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Jan 8, 2016, at 3:28 PM, Rob Mayoff via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Why are you even using throw and try-bang here? It looks like, instead of throw, you should be using preconditionFailure: “Use this function to stop the program when control flow can only reach the call if your API was improperly used.”<div class=""><br class=""><div class=""><div class=""><div class=""><a href="https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_StandardLibrary_Functions/index.html#//apple_ref/swift/func/s:FSs19preconditionFailureFTKT_SS4fileVSs12StaticString4lineSu_T_" class="">https://developer.apple.com/library/ios/documentation/Swift/Reference/Swift_StandardLibrary_Functions/index.html#//apple_ref/swift/func/s:FSs19preconditionFailureFTKT_SS4fileVSs12StaticString4lineSu_T_</a><br class=""></div><div class=""><br class=""></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Thu, Jan 7, 2016 at 4:33 PM, Don Wills via swift-users <span dir="ltr" class=""><<a href="mailto:swift-users@swift.org" target="_blank" class="">swift-users@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word" class=""><div class=""><br class=""></div><div class=""><font face="Courier New" class=""><b class="">func add(p1: String, _ p2 Any) {</b></font></div><div class=""><font face="Courier New" class=""><b class=""> try! addWithThrows(p1, p2)</b></font></div><div class=""><font face="Courier New" class=""><b class="">}</b></font></div><div class=""><font face="Courier New" class=""><b class=""><br class=""></b></font></div><div class=""><font face="Courier New" class=""><b class="">private func addWithThrows(p1: String, _ p2 Any) throws {</b></font></div><div class=""><font face="Courier New" class=""><b class=""> // code that handles many types for p2, followed by return for each of the valid types</b></font></div><div class=""><font face="Courier New" class=""><b class=""> throw Exception("Invalid type") /* for those types not supported which is a programmer error */</b></font></div><div class=""><font face="Courier New" class=""><b class="">}</b></font></div><div class=""><br class=""></div></div></blockquote></div></div></div></div></div></div>
<img src="http://portal.mxlogic.com/images/transparent.gif" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-users mailing list<br class=""><a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-users<br class=""></div></blockquote></div><br class=""></div></div></div></div></body></html>