Java မှာ Keyword နဲ့ ပစ် တယ်။
ဥပမာ
အသက် 18 နှစ်အောက် ဖြစ်ပါက ခြွင်းချက်တစ်ခုအား ပစ် ပါ ("ဝင်ရောက်ခွင့်ကို ငြင်းဆိုထားသည်")။ အသက် 18 နှစ်နှင့်အထက်ဖြစ်ပါက "Access granted" ကို ရိုက်နှိပ်ပါ။
public class Main {
static void checkAge(int age) {
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...)
}
}
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
throw
စိတ်ကြိုက်အမှားဖန်တီးရန် အဓိကစကားလုံးကို အသုံးပြုသည် ။
ဖော်ပြချက်ကို ခြွင်းချက်အမျိုးအစားthrow
တစ်ခုနှင့် တွဲသုံးသည် ။ Java တွင်ရရှိနိုင်သောခြွင်းချက်အမျိုးအစားများစွာရှိသည်- , , , , etc.ArithmeticException
ClassNotFoundException
ArrayIndexOutOfBoundsException
SecurityException
ခြွင်းချက်အမျိုးအစားကို အထက်ဖော်ပြပါ ဥပမာတွင်ကဲ့သို့ စိတ်ကြိုက် နည်းလမ်းတစ်ခု နှင့် တွဲသုံးလေ့ရှိသည် ။
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 တွင် ခြွင်းချက်များအကြောင်း ပိုမိုဖတ်ရှုပါ ။