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) को नहीं। Interviews को यह अंतर बहुत पसंद है।

Challenge

+30 XP

दो values 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.