NEW: Learn SwiftUI with my free YouTube video series! >>

How to force a crash using fatalError()

Swift version: 5.0

Paul Hudson    @twostraws   

Swift has a built-in function called fatalError(), which forces your application to crash. This might sound useful, but bear with me – this is an indispensable function for anyone serious about writing good Swift.

The fatalError() function has a special return type called Never, which Swift understands as meaning execution will never continue after this function has been called. As a result, you can use fatalError() in methods that return a value but you have nothing sensible to return.

For example, the cellForRowAt method must return a UITableViewCell, but what happens if you dequeue a reusable cell and try to conditionally typecast it to your custom cell type, only for that to fail?

Normally you might try to return an empty, unconfigured cell, but that doesn’t really make much sense – if you got a bad cell back you have a bug, and trying to limp along will just cause issues.

Fortunately, fatalError() can fix that: if your typecast fails you can call fatalError() with a message explaining what happened, and if the typecast fails your app will terminate.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? MyCustomCell else {
        fatalError("Failed to load a MyCustomCell from the table.")

    return cell

Obviously you never want that code to get hit in production, but using fatalError() helps stop that from happening – you will now get a very obvious problem in development if things aren’t going well.

SPONSOR Meet the new Instabug – more than just bug reporting! We help you build better apps and minimize your debugging time. With each bug report, we automatically capture details like network requests, repro steps, and session details. Get real-time crash reports with stack trace details and session data to help you catch and fix issues easily. And with our customizable in-app surveys, you’ll gather insightful user feedback and much more. Instabug is the fastest and easiest way to release with confidence. Start your free trial now! Start your free trial now!

Available from iOS – learn more in my book Pro Swift

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Average rating: 5.0/5

Click here to visit the Hacking with Swift store >>