swift 錯誤處理do catch try try!使用詳解
在swift中 如果我們要定義一個表示錯誤類型非常簡單,只要遵循Error協(xié)議就可以了,我們通常用枚舉或者結(jié)構(gòu)體來表示錯誤類型,枚舉可能用的多些,因為他能更直觀的表達當(dāng)前錯誤類型的每種錯誤細節(jié)。
//
// AboutError.swift
// learn_swiftUi
//
// Created by liuan on 2020/9/4.
// Copyright ? 2020 liuan. All rights reserved.
//
import Foundation
enum VendingMachingError:Error{
case invalideSelection
case insufficientFunds(coinsNeeded:Int)
case outOfStock
}函數(shù)、方法和初始化器都可以拋出錯誤。需要在參數(shù)列表后面,返回值加throws 關(guān)鍵字。
簡化版
func canThrowErrors() throws -> String
//
// AboutError.swift
// learn_swiftUi
//
// Created by liuan on 2020/9/4.
// Copyright ? 2020 liuan. All rights reserved.
//
import Foundation
enum VendingMachingError:Error{
case invalideSelection
case insufficientFunds(coinsNeeded:Int)
case outOfStock
}
struct Item {
var price: Int
var count: Int
}
class VendingMathine{
var inventory=[
"Candy bar":Item(price: 12, count: 7),
"Chips":Item(price: 10, count: 4),
"Pretzels":Item(price: 7, count: 11),
]
var coinsDeposited = 0
func vend(itemNaamed name:String) throws{
guard let item = inventory[name] else{
throw VendingMachingError.invalideSelection
}
guard item.count > 0 else{
throw VendingMachingError.outOfStock
}
guard item.price <= coinsDeposited else{
throw VendingMachingError.insufficientFunds(coinsNeeded: item.price - coinsDeposited)
}
coinsDeposited -= item.price
var newItem = item
newItem.count -= 1
inventory[name]=newItem
print("Dispensing \(name)")
}
}
let vm=VendingMathine()
vm.coinsDeposited=2
try vm.vend(itemNaamed: "Pretzels")拋出異常
Playground execution terminated: An error was thrown and was not caught:
? VendingMachingError
? insufficientFunds : 1 element
- coinsNeeded : 5在Swift中我們使用do-catch塊對錯誤進行捕獲,當(dāng)我們在調(diào)用一個throws聲名的函數(shù)或方法時,我們必須把調(diào)用語句放在do語句塊中,同時do語句塊后面緊接著使用catch語句塊

do里面執(zhí)行調(diào)用語句
后面跟著catch 第一種錯誤 在第一種錯誤里面做處理
第二種錯誤需要符合一定的條件
然后做錯誤處理
第三種是沒有捕獲到的錯誤 然后在第三個語句里面做處理

如果你確信一個函數(shù)或者方法不會拋出錯誤,可以使用try! 來中斷錯誤的傳播,但是如果錯誤真的發(fā)生了。你會得到一個運行時錯誤
// // AboutError.swift // learn_swiftUi // // Created by liuan on 2020/9/4. // Copyright ? 2020 liuan. All rights reserved. // import Foundation let photo = try! 5/0
warning: BlackMyPlayground.playground:10:19: warning: no calls to throwing functions occur within 'try' expression
let photo = try! 5/0
^
warning: BlackMyPlayground.playground:10:19: warning: no calls to throwing functions occur within 'try' expression
let photo = try! 5/0
^
error: BlackMyPlayground.playground:10:19: error: division by zero
let photo = try! 5/0
^defer關(guān)鍵字:defer block 例的代碼會在函數(shù)return之前執(zhí)行,無論函數(shù)是從哪個分之return的,還有throw,還是自然而然走到最后一樣。
//
// File.swift
// learn_swiftUi
//
// Created by liuan on 2020/9/4.
// Copyright ? 2020 liuan. All rights reserved.
//
import Foundation
func processFile(fileName: String)throws{
defer{
print("JIESHU")
}
print("KAISHI ")
}到此這篇關(guān)于swift 錯誤處理do catch try try!使用詳解的文章就介紹到這了,更多相關(guān)swift 錯誤處理do catch try try!內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Swift編程中用以管理內(nèi)存的自動引用計數(shù)詳解
這篇文章主要介紹了Swift編程中用以管理內(nèi)存的自動引用計數(shù)詳解,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-11-11
Swift開發(fā)之使用UIRefreshControl實現(xiàn)下拉刷新數(shù)據(jù)及uirefreshcontrol使用
本文給大家介紹使用UIRefreshControl實現(xiàn)下拉刷新數(shù)據(jù),及UIRefreshControl的使用步驟,對本文感興趣的朋友一起學(xué)習(xí)吧2015-11-11
Swift實現(xiàn)Selection Sort選擇排序算法的實例講解
選擇排序是一種穩(wěn)定的排序算法,且實現(xiàn)代碼通常比冒泡排序要來的簡單,這里我們就來看一下Swift實現(xiàn)Selection Sort選擇排序的實例講解2016-07-07
Swift中通知中心(NotificationCenter)的使用示例
這篇文章主要給大家介紹了關(guān)于Swift中通知中心(NotificationCenter)使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10

