Java သည် Keyword ကို ပစ်သည်။
ဥပမာ
အသက် 18 နှစ်အောက် ဖြစ်ပါက ခြွင်းချက်တစ်ခုအား ပစ် ပါ ("ဝင်ရောက်ခွင့်ကို ငြင်းဆိုထားသည်")။ အသက် 18 နှစ်နှင့်အထက်ဖြစ်ပါက "Access granted" ကို ရိုက်နှိပ်ပါ။
public class Main {
static void checkAge(int age) throws ArithmeticException {
if (age < 18) {
throw new ArithmeticException("Access denied - You must be at least 18 years old.");
}
else {
System.out.println("Access granted - You are old enough!");
}
}
public static void main(String[] args) {
checkAge(15); // Set age to 15 (which is below 18...)
}
}
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
သော့ချက်စကားလုံး သည် throws
နည်းလမ်းတစ်ခုဖြင့် ခြွင်းချက်အမျိုးအစားကို ချွင်းချက်ဖြစ်စေမည့်အရာကို ဖော်ပြသည်။
Java တွင်ရရှိနိုင်သောခြွင်းချက်အမျိုးအစားများစွာရှိသည်- ArithmeticException
, ClassNotFoundException
, ArrayIndexOutOfBoundsException
, SecurityException
, etc.
throw
နှင့် အကြား ကွာခြားချက်များ throws
:
throw | throws |
---|---|
Used to throw an exception for a method | Used to indicate what exception type may be thrown by a method |
Cannot throw multiple exceptions | Can declare multiple exceptions |
Syntax:
|
Syntax:
|
ဆက်စပ်စာမျက်နှာများ
ကျွန်ုပ်တို့၏ Java Try..Catch Tutorial တွင် ခြွင်းချက်များအကြောင်း ပိုမိုဖတ်ရှုပါ ။