-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path_5.ExceptionHandling.java
More file actions
34 lines (34 loc) · 1.05 KB
/
_5.ExceptionHandling.java
File metadata and controls
34 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
//defining an exception class
class InsufficientBalanceError extends Exception{
//defining constructor
InsufficientBalanceError(String msg){
System.out.println(msg);
}
}
class ExceptionHandling{
public static void main(String ar[]){
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to banking service");
System.out.println("````````");
System.out.println("Your bank balance is 2000 rupees!");
System.out.print("Enter the amount you want to withdraw:");
int amount = sc.nextInt();
//try block
try{
withdraw(amount);
System.out.println("Amount withdrawn!");
}
//if amount exceeds the balance
catch (Exception e){
System.out.println(e);
}
}
//withdraw method
static void withdraw(int amount) throws InsufficientBalanceError{
if(amount>2000)
{
throw new InsufficientBalanceError("No sufficient balance in your account!");
}
}
}