What are fatal errors and when to use them??

This is a continuation of my other article about error handling in swift.

Abhimuralidharan
Jul 26, 2017 · 4 min read
In the middle of no where!! where to go?? what to do?? leave a message and sink??

Do read this article before proceeding : Error Handling in swift.

Apple docs link: Fatal errors in swift.

Working: Unconditionally prints a given message and stops execution.

If you are in the middle of an ocean , Don’t know where to go?? Don’t know what to do??, the best option is to leave a message and sink!!! May be someone will find you — This is exactly what a fatal error will do.

Declaration:

fatal error declaration

Fatal error is a method which prints a given message on the console and stop the execution of the app when executed. The error will be more specific that it will have the file name and line number so that it is easy to debug.

From the declaration, we can see the parameters of fatalError(:) method . It accepts an @autoClosure which takes no arguments and return a string. So, the message can be any expression that returns a String. If you are not familiar with @autoClosure , please go through my other article about closures in swift.

The other two arguments includes a line number and file name which the compiler takes automatically. We don’t have to bother about that. We just have to pass a error message string.

This is what the function declaration looks like.

Never return type:

Never return type

Use Never as the return type when declaring a closure, function, or method that unconditionally throws an error, traps, or otherwise does not terminate.

There are functions which terminate the process immediately and do not return to the caller. These were marked in Swift with @noreturn attribute before swift 3. In swift 3, @noreturn attribute is replaced with Never return type.

Functions with Never as return type can also be used to mark cases that “should not occur” and indicate a programming error.

How to call a fatalError ??

Without a message:

fatalError()

I added the above line of code inside viewDidLoad method and this is what I got printed in the console when the fatalError() was executed.

2017–07–26 10:42:01.862958+0530 Calculator[1887:495386] fatal error: file /Users/abhilash/Desktop/Calculator/Calculator/ViewController.swift, line 95

With Error message:

fatalError(“Error: Unknown operation.”)

I added the above code in the viewDidLoad method and the console printed this:

2017–07–26 10:56:07.003120+0530 Calculator[1892:497747] fatal error: Error: Unknown operation.: file /Users/abhilash/Desktop/Calculator/Calculator/ViewController.swift, line 95

Fatal error tips:

  • You cannot catch a fatal error in a do-catch statement. And methods that may throw a fatal error don’t need to be marked as throwing.
  • In normal scenarios, we don’t expect the control flow to reach a fatalError method call. If reached, then that means the app is in an unknown state and we don’t want to run the app in an unknown state. So, the app will crash at this point and this is a good thing.
  • If you decide to use fatal errors, use them sparingly. Only use them if the application enters an unknown state, a state it doesn’t know how to handle.

I found a good example on where to use fatal errors in this article from cocoacasts.com.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {
if let section = Section(rawValue: indexPath.section) {
switch section {
case .info:
let cell = tableView.dequeueReusableCell(withIdentifier: infoCell, for: indexPath)
return configureInfoCell(cell)
case .profile:
let cell = tableView.dequeueReusableCell(withIdentifier: infoCell, for: indexPath)
return configureProfileCell(cell)
case .settings:
let cell = tableView.dequeueReusableCell(withIdentifier: infoCell, for: indexPath)
return configureSettingsCell(cell, withModel: settingsModel)
}
}

fatalError("Invalid Section")
}

That’s it . Now you know what a fatal error is and where to use them.

Enjoy!!

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Abhimuralidharan

Written by

iOS and tvOS developer, dreamer, photographer 🤨

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade