Error handling in Swift does not involve stack unwinding. What does it mean?
Stack unwinding is just the process of navigating up the stack looking for the handler. Wikipedia summarizes it as follows:
Some languages call for unwinding the stack as this search progresses. That is, if function
f, containing a handlerHfor exceptionE, calls functiong, which in turn calls functionh, and an exceptionEoccurs inh, then functionshandgmay be terminated, andHinfwill handleE.
Whereas a Swift error doesn't unwind the stack looking for a handler. It just returns, and expects the caller to handle the thrown error. In fact, the sentence after the one you quote goes on to say:
As such, the performance characteristics of a
throwstatement are comparable to those of areturnstatement.
So, using that first example, where f called g which calls h, in Swift, if you want f to catch the error thrown by h, then:
hmust explicitly be marked that itthrowserrors;gmust explicitlytryits call toh;gmust also be marked that itthrowserrors, too; andfmust explicitlytryits call tog.
In short, while some other languages offer stack unwinding in the process of finding the exception handler, in Swift error handling, you must either explicitly catch the error thrown by functions you try, or be designated as a function that throws so that failed try calls will be thrown back up to the caller. There is no automatic unwinding of the stack in Swift.
All of this is unrelated to the question of whether deallocation takes place. As you've seen, yes, the throw in Swift behaves much like return, deallocating those local variables.
It's worth noting that not all exception handling that involves stack unwinding does the deallocation. Generally it does (because of course we want it to clean up when we're handling exceptions), but for example, "the GNU C++ unwinder does not call object destructors when an unhandled exception occurs. The reason for this is to improve debuggability." (From Exception Handling in LLVM.) Clearly, that's only appropriate for unhandled exceptions in debugging environments, but it illustrates the issue that unwinding the stack doesn't necessarily mean that objects are deallocated.
https://stackoverflow.com/questions/46814233/error-handling-in-swift-does-not-involve-stack-unwinding-what-does-it-mean
Error handling in Swift does not involve stack unwinding. What does it mean?的更多相关文章
- Erlang error handling
Erlang error handling Contents Preface try-catch Process link Erlang-way error handling OTP supervis ...
- setjmp()、longjmp() Linux Exception Handling/Error Handling、no-local goto
目录 . 应用场景 . Use Case Code Analysis . 和setjmp.longjmp有关的glibc and eglibc 2.5, 2.7, 2.13 - Buffer Over ...
- Error Handling
Use Exceptions Rather Than Return Codes Back in the distant past there were many languages that didn ...
- 19 Error handling and Go go语言错误处理
Error handling and Go go语言错误处理 12 July 2011 Introduction If you have written any Go code you have pr ...
- Error Handling Functions(微软对于出错的情况下提供的所有函数,比如SetThreadErrorMode,SetErrorMode,SetLastErrorEx,FatalAppExit,CaptureStackbackTrace)
The following functions are used with error handling. Function Description Beep Generates simple ton ...
- ASP.NET Error Handling
https://docs.microsoft.com/en-us/aspnet/web-forms/overview/getting-started/getting-started-with-aspn ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions
The ideal time to catch an error is at compile time, before you even try to run the program. However ...
- MySQL Error Handling in Stored Procedures 2
Summary: this tutorial shows you how to use MySQL handler to handle exceptions or errors encountered ...
- Error Handling and Exception
The default error handling in PHP is very simple.An error message with filename, line number and a m ...
随机推荐
- linux -------------- Linux系统安装jdk
linux 安装软件有三种方式 tar (解压安装 ) rpm (直接安装) yum(在线) 安装主要步邹 1.下载jdk 软件包 2.检测是否安装 查看已安装jdk软件包 rpm -qa|grep ...
- Java并发编程: CountDownLatch、CyclicBarrier和 Semaphore
java 1.5提供了一些非常有用的辅助类来帮助并发编程,比如CountDownLatch,CyclicBarrier和Semaphore. 1.CountDownLatch –主线程阻塞等待,最后完 ...
- markdown使用emoji
前几日写博客的时候在想是否能够在markdown中使用emoji呢
- Windows定时清理文件处理脚本
一.运行CMD,输入forfile/?,即可获取forfile的使用方法 /P 路径 /M 文件类型 /D 时间 + | - +:之后 - :之前 example:-2 ...
- 【C#常用方法】3.将DataTable一次性插入数据库表中(使用SqlBulkCopy)
将DataTable一次性插入数据库表中(使用SqlBulkCopy) 1.SqlBulkCopy简介 SqlBulkCopy类是ADO.NET中专门用于数据库批量插入数据的类,其批量插入的执行速度是 ...
- python中用分别用selenium、requests库实现Windows认证登录
最近在搞单位的项目,实现python自动化,结果在第一步就把我给拒之门外,查资料问大佬,问我们开发人员,从周一折腾到周五才搞定了 接下给大家分享一下 项目背景:我们系统是基于Windows平台实现的, ...
- Django---图书管理系统,多对多(ManyToMany),request获取多个值getlist(),反查(被关联的对象.author_set.all)
Django---图书管理系统,多对多(ManyToMany),request获取多个值getlist(),反查(被关联的对象.author_set.all) 一丶多对多查询 表建立多对多关系的方式 ...
- Node.js 连接 MongoDB数据库
安装指令:npm install mongodb var mongodb = require("mongodb");// console.log(mongodb); var Mon ...
- android shap画圆(空心圆、实心圆)
实心圆: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=" ...
- elementUI一次请求上传多个文件
elementui <el-upload class="upload-demo" ac ...