How to throw an error/exception with a custom message in Swift (simple way)?
Hi everyone! I just started learning Swift and I can't figure out error handling. In other languages, like Python, I can just write `raise Exception("My message")` and that's it. But in Swift, it seems complicated.
I need to throw an error with my own custom text at a specific point in the code when a certain condition is met. For example, I'm checking the user's age:
```swift
let age = 15
if age < 18 {
// need to throw an error with the message "User is underage"
}
```
I've read about `do-catch`, `try`, but everywhere I need to create my own error types using `enum` and implement the `Error` protocol. This seems too cumbersome for a simple situation. Isn't there a way to just say "throw this string as an error"?
I tried
No answers yet.
Log in to leave an answer