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.