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.