How to capture and handle an Exception in Swift language

0

I have a method of a framework , of AVFunction that throws an exception, but when I write the code that calls this function I can not figure out what exactly the exception is thrown.

  • How do I proceed to find the possible Exceptions ?
  • How should I treat this Exceptions ?
asked by anonymous 19.09.2015 / 22:23

1 answer

1

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

There are functions and methods that document your exceptions in the signature itself which can make it easier since the compiler will inform you that you forget to handle the exception. But this type of exception is controversial, abused and often comes the wrong way just because it's bound.

Conscious capture of exceptions is the only appropriate way. This will only occur with the developer's willingness to read the documentation.

There is a documentation about 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
}

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

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

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

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

    
19.09.2015 / 22:56