Swift try-catch exception

Is it possible to catch exceptions in Swift? Given the following code:

NSException.raise(NSRangeException, format: "Now you've gone too far!", arguments: CVaListPointer(fromUnsafePointer: UnsafePointer())) 

Is it possible to prevent exclusion from an entire program? That is, which is equivalent to Swift from the following in Objective-C:

 @try { [NSException raise:NSRangeException format:@"Now you've gone too far!"]; } 
+27
exception try-catch swift
Jun 03 '14 at 19:12
source share
2 answers

There is no exception handling in it , and this discussion on the developer forum discusses why this might be:

but keep in mind that Cocoa and Cocoa Touch traditionally do not intend for you to catch exceptions; they want you not to be abandoned in the first place. Common errors should be handled using the optional types and inout parameters of NSError; you should address any situation that leads to a failure of the statement (which seems to be the only exception mechanism in Swift) by writing better code.

+10
Jun 03 '14 at 19:19
source share

I believe that Swift does not support this today. Most likely, this will be added to future beta.

+1
Jun 03 '14 at 19:17
source share



All Articles