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.