Swift exception capture use of Try Catch

Source: Internet
Author: User
Tags instance method try catch

The new anomaly capture mechanism is updated with the new Swift2.0 released on WWDC 2015, and the unusual try-catch mechanism helps us locate the cause of the error faster, and here is a brief explanation of the use of the try-catch mechanism. 1. Defining enumeration Outliers First we can create our own exception enumeration value, which is to implement the ErrorType protocol, but this protocol is empty, we add just fine. 2. Throw an exception we can throw an exception in the method, it is very simple to define the method that throws the exception, just add throws to the front of the return value, the example is as follows: (This method is an instance method in a custom dog class, Name.length () Is the method that is added for the system's string class by extension)


3. Catch exception we can catch the exception by adding a catch keyword after the method that may throw the exception is called

4. Do not handle exceptions if you determine that a piece of code that might throw an exception is not likely to throw an exception, you can use try! to make the call, but if the code throws an exception, it will cause a runtime exception error, so use caution. End: It's worth noting that in a method that might throw an exception, Once an exception is thrown in one place, the method stops running immediately. The code behind the thrown exception code will not be executed with custom dog classes and code that calls the Dog class enum dogerror:errortype{
Case Nameinvaliderror
Case Ageinvaliderror
Case Namelengtherror
}

Class Dog:nsobject {
var name:string!
var age:int!
Init (name:string,age:int) {
Self.name = Name
Self.age = Age
}

Func Printdogmessage (Dog:dog) throws-string{
Print ("Dog-class: Start checking if name is invalid")
Guard Let name:string = Dog.name else{
Throw Dogerror.nameinvaliderror
}
Print ("Dog-class: Start checking if the name length is invalid")
If Name.length () < 3{
Throw Dogerror.namelengtherror
}
Print ("Dog-class: Start checking if age is invalid")
If age < 0 | | Age > 20{
Throw Dogerror.ageinvaliderror
}
Print ("Dog-class: Return message") return "dog information for" + Dog.name + "\ (dog.age)}} Test method code: Let Mydog = Dog (name:" Small white ", a        GE:21) var str:string! do{
Try str = mydog.printdogmessage (Mydog)
}
catch {
Switch (Error) {
Case DogError.NameInvalidError:print ("Dog's name Invalid error")
Case DogError.NameLengthError:print ("Dog's name length error")
Case DogError.AgeInvalidError:print ("Dog's age setting is not legal")
Default:print ("Main: Catch to Dog other error")
}
} print ("Main: Last output is \ (str)")

Swift exception capture use of Try Catch

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.