🛠️ 9. Try / Catch - Error Handling

🔹 1. Try / Catch (Synchronous)

Jab aapko lagta hai koi line error throw kar sakti hai, use `try` block me rakho. Agar error aaye, to `catch` block usko handle karega.

try {
  let result = 10 / 0;
  console.log(result);
} catch (error) {
  console.error("Error aya:", error);
}

🔹 2. Try / Catch (Async + Await)

Async code me `await` ke errors bhi `try/catch` ke zariye pakre ja sakte hain.

async function fetchData() {
  try {
    const res = await fetch("https://fakeapi.io/data");
    const data = await res.json();
    console.log(data);
  } catch (err) {
    console.error("Fetch failed:", err);
  }
}

fetchData();

🔹 3. Throw Custom Error

Aap manually bhi error `throw` kar sakte ho kisi condition pe.

function divide(a, b) {
  if (b === 0) {
    throw new Error("0 se divide nahi kar sakte!");
  }
  return a / b;
}

try {
  console.log(divide(5, 0));
} catch (e) {
  console.error("Custom error:", e.message);
}