程式使用try…catch 時機

程式使用try…catch 時機

程式使用try…catch 時機


資料來源: 自己心得

01.接收輸入/輸出 資料 (電器產品的保護電路)


02.型態轉換


03.對外介接(DLL/網路/檔案/共享FIFO/…)

PS.會拖慢效率

5 thoughts on “程式使用try…catch 時機

  1. PHP

    try {
    // 可能會拋出異常的代碼
    throw new Exception("An error occurred");
    } catch (Exception $e) {
    echo "Caught exception: " . $e->getMessage();
    } finally {
    echo "This will always execute";
    }

  2. JavaScript

    try {
    // 可能會拋出異常的代碼
    throw new Error("An error occurred");
    } catch (e) {
    console.log("Caught exception: " + e.message);
    } finally {
    console.log("This will always execute");
    }

  3. Python

    try:
    # 可能會拋出異常的代碼
    raise Exception("An error occurred")
    except Exception as e:
    print(f"Caught exception: {e}")
    finally:
    print("This will always execute")

  4. Java


    public class Main {
    public static void main(String[] args) {
    try {
    // 可能會拋出異常的代碼
    throw new Exception("An error occurred");
    } catch (Exception e) {
    System.out.println("Caught exception: " + e.getMessage());
    } finally {
    System.out.println("This will always execute");
    }
    }
    }

  5. C++

    #include

    int main() {
    try {
    // 可能會拋出異常的代碼
    throw std::runtime_error("An error occurred");
    } catch (const std::exception& e) {
    std::cout << "Caught exception: " << e.what() << std::endl;
    } finally {
    std::cout << "This will always execute (C++ doesn't have finally, use RAII instead)" << std::endl;
    }
    return 0;
    }

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *