What are fatal errors and when to use them??

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

Image for post
Image for post
In the middle of no where!! where to go?? what to do?? leave a message and sink??

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:

Image for post
Image for post
fatal error declaration
Image for post
Image for post
This is what the function declaration looks like.

Never return type:

In the above function declaration, you can see the function returns a type Never . The documentation about the Never type is given below.

Image for post
Image for post
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 ??

We use function in one of two ways:

Without a message:

fatalError()
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.”)
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:

  • Fatal errors are typically thrown when the application enters an unknown or unexpected state.
  • 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.
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")
}

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

You can follow me on Medium for fresh articles. Connect with me on LinkedIn.

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store