ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်
Project ရဲ့ နောက်ဆုံး Part ဖြစ်ပါတယ်။ Exception handling ကို အသုံးပြုပြီး invalid data (ဥပမာ score က 0-100 range ပြင်ပ) ရောက်လာရင် program crash မဖြစ်ဘဲ graceful ဖြစ်အောင် handle လုပ်ပါမယ်။ Custom exception class InvalidScoreException ကို Exception ကနေ extend လုပ်ပြီးဆောက်ပါမယ်။ Comparator interface ကိုသုံးပြီး student list ကို average score အလိုက် sort လုပ်တာလည်း ဒီ Part ထဲမှာ ထည့်ပါမယ်။ နောက်ဆုံးမှာ student data အားလုံးကို summary table အနေနဲ့ report ထုတ်ပြီး project ပြီးဆုံးပါလိမ့်မယ်။
လက်တွေ့ ဆောက်ကြည့်မယ်
InvalidScoreException class ကို Exception ကနေ extend လုပ်ပြီး constructor ထဲမှာ super(message) ကို ခေါ်ပါ။ StudentManager class ထဲမှာ addScoreSafe(Student s, String subject, int score) method ရေးပြီး score က 0-100 range ပြင်ပဆို InvalidScoreException throw လုပ်ပါ - method signature မှာ throws InvalidScoreException ထည့်ဖို့ မမေ့ပါနဲ့။ printReport() method ထဲမှာ students.sort() နဲ့ Comparator.comparingDouble(Student::calculateAverage).reversed() ကိုသုံးပြီး average အများဆုံးက အပေါ်ဆုံးလာအောင် sort လုပ်ပါ၊ ပြီးရင် System.out.printf() နဲ့ formatted table print ထုတ်ပါ။ main method ထဲမှာ try-catch block နဲ့ addScoreSafe() ကို ခေါ်ပြီး invalid score တစ်ခု ကြိုးစားထည့်ကာ error message catch လုပ်တာကို စမ်းကြည့်ပါ။
Code နမူနာ
import java.util.ArrayList;
import java.util.Comparator;
class InvalidScoreException extends Exception {
public InvalidScoreException(String message) {
super(message);
}
}
class StudentManager {
private ArrayList<Student> students = new ArrayList<>();
public void addStudent(Student s) {
students.add(s);
}
public void addScoreSafe(Student s, String subject, int score) throws InvalidScoreException {
if (score < 0 || score > 100) {
throw new InvalidScoreException(subject + " score must be 0-100, got: " + score);
}
s.addScore(subject, score);
}
public void printReport() {
students.sort(Comparator.comparingDouble(Student::calculateAverage).reversed());
System.out.println("---- Final Report ----");
for (Student s : students) {
System.out.printf("%-10s Avg: %.1f Grade: %s%n",
s.getName(), s.calculateAverage(), s.getGrade());
}
}
}
public class StudentApp {
public static void main(String[] args) {
StudentManager manager = new StudentManager();
Student s1 = new Student("Aung Aung", 101);
Student s2 = new Student("Su Su", 102);
manager.addStudent(s1);
manager.addStudent(s2);
try {
manager.addScoreSafe(s1, "Math", 95);
manager.addScoreSafe(s2, "Math", 120); // invalid
} catch (InvalidScoreException e) {
System.out.println("Error: " + e.getMessage());
}
manager.printReport();
}
}Console မှာ Error: Math score must be 0-100, got: 120 ဆိုတဲ့ error message ပြီးရင် student list ကို average descending order နဲ့ Final Report table အနေနဲ့ print ထုတ်ပေးပါလိမ့်မယ်။၅ မိနစ် စမ်းကြည့်
InvalidScoreException အပြင် student name ကို empty string ထည့်ရင် throw လုပ်မယ့် InvalidNameException custom exception ကို ထပ်ဆောက်ပြီး 5 minutes အတွင်း integrate လုပ်ကြည့်ပါ။
သတိလေးတစ်ချက်
Custom exception တွေက checked exception (Exception ကနေ extend) ဖြစ်ရင် caller side မှာ try-catch ဒါမှမဟုတ် throws declaration လိုအပ်ကြောင်း သတိထားပါ။