How to use try and catch in special cases

in this code, try/catch inside ProcInfo() when using a timer to start or even when using DispatchQueue.global, it crashes when it throws an exception regardless the reason for exception is. Even if 0 information found and then it throws due to that. Got Bad Access error. Had to block out the try/catch, modified the design a bit.

@objc func ProcInfo() {
    DispatchQueue.global(qos: .default).async {

	/*do {
        try*/ var f = aSingletonClass.shared.GetInfo() //

        DispatchQueue.main.async {
          //update UI

	 /*} catch {
      ////
     }
     */

How to initialize this Info Struct inside the init() or outside of the structure?. had problem with initializing the a: OpaquePointer.

public struct Info {

    let a: OpaquePointer
    let b: Int

    init () {
    // how to initialize a?.
    }
}

no ideas?.

no ideas?.

I read your question yesterday and then re-read it today. My conclusion: There’s not enough info here for me to offer any insight. Is this Info structure something you created? If so, why are you using OpaquePointer for a? What’s a actually for?

it crashes when it throws an exception regardless the reason for
exception is.

The word exception is heavily overloaded, and exceptions aren’t the same as errors in Swift. For an explanation of this, see my What is an exception? post on DevForums.

I suspect that you’re triggering a machine exception, which isn’t something you can catch in Swift. And that’s likely because of your use of OpaquePointer, which is an unsafe construct (despite not having Unsafe in the name) and thus can easily trigger machine exceptions.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes

I put the break point and it even throws when there is no info available, like

pseudo:
like if number of Info is equal to 0 then
throw error.NoInfo

how to use try/catch block safely in that function so that it would not crash, it would exit gracefully.

how to use try/catch block safely in that function so that it would
not crash

It’s hard to answer questions like this without seeing more code — and specifically an explanation for why you’re using OpaquePointer per my previous post — but if this is, as I suspect, a machine exception, there’s no reasonable way to catch that in Swift.

If you post more details about what you’re doing, ideally a small code sample that I can actually compile and run, I should be able to give you better advice.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes