Home > Enterprise >  How can I detect an error when creating a thread in swift?
How can I detect an error when creating a thread in swift?

Time:10-04

How can I catch an error when creating a thread? I tried doing it like this but It hasn't worked. It says that catch is unreachable. Do I just check if the thread is executing?

@objc func threadmain(){
    do{
         /*Doing something*/
    }
    catch{
          print("More errors here")
    }
}
func this(){
    do{
        myThread = Thread (target: self, selector: #selector(threadmain), object: nil)
        try myThread.start()
    } catch {
        print("There was an error with mythread")
    }
}

CodePudding user response:

There is nothing inside

myThread.start()

That throws an error , so you can't do/catch it , this over testing isn't needed anymore , BTW use myThread.isExecuting right below the start line

  • Related