What are fatal errors and when to use them??

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

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:

fatal error declaration
This is what the function declaration looks like.

Never return type:

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()
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:

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 💚💚💚💚💚💚 !!!!

iOS and tvOS developer, dreamer, photographer 🤨