Java Exception Handling Exercise 2

Source: Internet
Author: User

Set up the exception package, set up a bank class, the class has a variable double balance represents a deposit, the bank class construction method can increase the deposit, the bank class has the withdrawal method withdrawal (double damount), when the amount of withdrawals is greater than the deposit , throws the insufficientfundsexception, the withdrawal amount is negative, throws Nagativefundsexception, if the new bank (100), represents the deposit bank 100 yuan, when uses the method withdrawal (150), Withdrawal (-15) throws a custom exception.

Package exception; Public classBank {Private Doublebalance;  PublicBank (Doublebalance)            {super ();  This. Balance =balance; }         Public voidWithDrawal (DoubleDamount) throws Insufficientfundsexception,nagativefundsexception {if(damount>balance) {                Throw Newinsufficientfundsexception (); }            if(damount<0)            {                Throw Newnagativefundsexception (); }        }                 Public Static voidMain (string[] args) {Bank T=NewBank ( -); Try{T.withdrawal ( the); }Catch(Exception e) {e.printstacktrace (); }            Try{T.withdrawal (- -); }Catch(Exception e) {e.printstacktrace (); }        }    }
 package exception;  public  class   Nagativefundsexception extends Exception { public   String GetMessage () { return    Withdrawals cannot be negative   ; }}
Package exception;  Public class Insufficientfundsexception extends Exception {    public  String getMessage ()     {          return " Insufficient Balance " ;     }}

Java Exception Handling Exercise 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.