Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Doesn't that mean typing try a million times instead of putting potentially throwable calls inside one try block and a bunch of different catch handlers?

I wonder why they insist on that. Perhaps it is easier to parse?



I suspect it's useful to visually signal that the call can fail, and make it obvious what's happening; after all, the "throws" annotation is at the target site, not on the caller end.

You need to prefix anything that throws with a "try":

    func mightThrow() throws {
      try mightAlsoThrow();
    }
Other than that, it looks exactly like C++ exceptions: Propagation happens along the chain of functions marked as "throws", until a "catch" handler intercepts it.

"try" is an expression, by the way, so you can do things like:

    let line = try file.readline()
There's also a "try!" statement that throws a runtime exception if the inner statement throws.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: