How to capture and treat an Exception in the Swift language

Asked

Viewed 185 times

0

I have a method of one framework, of AVFunction that throws an exception, but when writing the code that calls this function I can’t find what exactly the exception is launched.

  • How should I proceed to discover the possible Exceptions?
  • How should I treat this Exceptions?
  • This article has helped me a lot, but I haven’t been able to figure out how to find possible Exceptions when they are undocumented and you don’t have the code: https://www.hackingwithswift.com/new-syntax-swift-2-error-handling-try-catch

  • You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Something needs to be improved?

1 answer

1


To find out the possible exceptions you have to read the documentation of what you will use. Any other attempt will not solve.

There are functions and methods that document your exceptions in the signature itself which can facilitate since the compiler will inform you that you forget to treat the exception. But this kind of exception is controversial, abused and often treats the wrong way just because it’s required.

Conscious capture of exceptions is the only appropriate means. This will only occur with the will of the programmer and reading of the documentation.

There’s a documentation on the subject.

The most traditional way is:

do {
    try funcao()
    //faz algo aqui
} catch TipoErro.ErroEspecifico { //pega exceção específica
    //faz algo aqui
} catch TipoErro.ErroEspecifico where x == 0 { //exceção filtrada
    //faz algo aqui
} catch { //pega qualquer exceção não especificada anteriormente
    //faz algo aqui
}

I put in the Github for future reference.

In Swift exception are not special types, they can be represented by several forms. One of the most common is an enumeration derived from ErrorType.

There are a number of other ways to capture exceptions but this is the most traditional way. I would need more specific questions.

There are some very interesting forms in the language that it would be useful to have other languages.

Be careful not to overdo catching the exceptions that you can’t do anything useful. I I talk a lot about abuse.

  • I didn’t find it in the documentation, I went to the source and didn’t find it. Of course it must be somewhere, I just haven’t found it yet.

  • in the above case it seems not to solve the last catch, the correct one would be to put a low dash of type } catch _ {}, ai solves for any exception.

  • I read the suggested documentation before asking this question.

  • 1

    @Delfino is what I know about it. I could write more but then I would practically be bringing the documentation (not only the linked) for here. I could have more information but the answer would become a complete tutorial. If you have more specific questions, ask. I haven’t finished writing the last catcheven, I tidied up now but I’m not sure when you need the _ and when you don’t need it, there are two forms of catch all.

  • Thank you @bigown, really my doubts are just the two presented, how to discover the exceptions when undocumented and not described in the code and how to treat them, the second how treated them helped me a lot, and I’m reading all the documentation, but sometimes the problem arises before reaching the point of documentation that deals with the subject. Thank you again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.