Lesson 31 of 34

Exceptions: Errors সামলানো

10 min 30 XP

প্রোগ্রাম চলার সময় error (exception) হলে সেটি crash করে। ঝুঁকিপূর্ণ কোড try-এর ভেতরে রাখুন এবং error catch-এ সামলান। finally সবসময় চলে।

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

throw দিয়ে নিজেই exception তুলতে পারেন, যেমন যখন কোনো method ভুল value পায়।

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

Checked exceptions (যেমন IOException) compile time-এ সামলাতে হয়; unchecked (RuntimeException) নয়। Interview-এ এই পার্থক্য খুব জনপ্রিয়।

Challenge

+30 XP

দুটি value text হিসেবে পড়ুন (sc.next()), Integer.parseInt দিয়ে বদলে প্রথমটিকে দ্বিতীয়টি দিয়ে ভাগ করে print করুন। কোনোটি সংখ্যা না হলে "Invalid number" এবং 0 দিয়ে ভাগে "Cannot divide by zero" print করুন। Input: 20 4 → Output: 5

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.