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.