منتدى مصر التقني
هل تريد التفاعل مع هذه المساهمة؟ كل ما عليك هو إنشاء حساب جديد ببضع خطوات أو تسجيل الدخول للمتابعة.

تمارين بلغة الجافا...إنشاء استثناء Exception خاص بك

اذهب الى الأسفل

تمارين بلغة الجافا...إنشاء استثناء Exception  خاص بك  Empty تمارين بلغة الجافا...إنشاء استثناء Exception خاص بك

مُساهمة من طرف أحمد مناع السبت مايو 16, 2020 11:52 am

السؤال :

الكود:

class Account
{
private double balance;


public double deposit(double amount)
{
if (amount > 0)
    balance += amount;
else
  return -1;  
return balance;
}


}



rewrite the class so that it throws appropriately exception instead of returning 1 as an error code write test code that attempts to deposit invalid amounts and catches the exceptions that are thrown



الحل :

الكود:

import java.util.logging.Level;
import java.util.logging.Logger;

class Account
{
private double balance;


public double deposit(double amount) throws Exception
{
if (amount > 0)
    balance += amount;
else
    throw new Exception ("The balance is insufficient");
return balance;
}


}

public class test1 {

 
    public static void main(String[] args) {
       
    Account myAccount = new Account();
   
        try {
            myAccount.deposit(0);
        } catch (Exception ex) {
          System.err.println(ex.getMessage());
        }
       
    }
   
}

ـــــــــــــــــــ التوقيع ــــــــــــــــــــ
سبحان الله وبحمدة .....سبحان الله العظيم
أحمد مناع
أحمد مناع
.
.

تاريخ التسجيل : 15/02/2011
المساهمات : 1108
النقاط : 202034
التقيم : 144
الدولة : مصر
الجنس : ذكر

https://egy-tech.forumegypt.net

الرجوع الى أعلى الصفحة اذهب الى الأسفل

الرجوع الى أعلى الصفحة

ََ

مواضيع ذات صلة


 
صلاحيات هذا المنتدى:
لاتستطيع الرد على المواضيع في هذا المنتدى