Skip to content

Commit 4538a0b

Browse files
committed
User Define Exceptions Handling
1 parent 2521325 commit 4538a0b

File tree

5 files changed

+187
-0
lines changed

5 files changed

+187
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.*;
2+
3+
class ExceptionHandling {
4+
5+
public static void main(String[] args)throws IOException {
6+
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+
System.out.println("Enter Integer Value : ");
10+
int data = 0;
11+
try {
12+
13+
data = Integer.parseInt(br.readLine());
14+
} catch(NumberFormatException obj) {
15+
16+
System.out.println("Please Enter Integer Data");
17+
}
18+
System.out.println(data);
19+
}
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
class PrintException {
4+
5+
public static void main(String[] args) {
6+
7+
Scanner sc = new Scanner(System.in);
8+
9+
System.out.println("Enter Integer Value : ");
10+
int num = 0;
11+
12+
try {
13+
14+
num = sc.nextInt();
15+
} catch(InputMismatchException obj) {
16+
17+
System.out.print("Exception in Thread " + "\"" +Thread.currentThread().getName() + "\"" + " ");
18+
obj.printStackTrace();
19+
}
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.*;
2+
3+
class MultipleException {
4+
5+
public static void main(String[] args) {
6+
7+
Scanner sc= new Scanner(System.in);
8+
9+
System.out.println("Enter Integer Value : ");
10+
int num = 0;
11+
12+
try {
13+
14+
num = sc.nextInt();
15+
System.out.println(10/num);
16+
} catch(InputMismatchException | ArithmeticException obj) {
17+
18+
System.out.println("Exception Handled");
19+
}
20+
}
21+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import java.io.*;
2+
3+
class DataUnderFlowException extends RuntimeException{
4+
5+
DataUnderFlowException(String msg) {
6+
7+
super(msg);
8+
}
9+
}
10+
11+
class DataOverFlowException extends RuntimeException{
12+
13+
DataOverFlowException(String msg) {
14+
15+
super(msg);
16+
}
17+
}
18+
19+
class Main {
20+
21+
public static void main(String[] args) throws IOException {
22+
23+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
24+
25+
int arr[] = new int[5];
26+
27+
System.out.println("Enter Integer value in Array : ");
28+
System.out.println("Note: 0 < element < 100");
29+
for(int i = 0; i < arr.length; i++) {
30+
31+
int data = 0;
32+
try {
33+
34+
data = Integer.parseInt(br.readLine());
35+
} catch(NumberFormatException nfe) {
36+
37+
System.out.println("Enter Integer Value : ");
38+
data = Integer.parseInt(br.readLine());
39+
}
40+
41+
if(data < 0) {
42+
43+
try {
44+
45+
throw new DataUnderFlowException("Data is less than 0");
46+
} catch(DataUnderFlowException duf) {
47+
48+
System.out.println(duf.getMessage());
49+
i--;
50+
}
51+
}
52+
if(data > 100) {
53+
54+
try {
55+
56+
throw new DataOverFlowException("Data is greater than 100");
57+
} catch(DataOverFlowException duf) {
58+
59+
System.out.println(duf.getMessage());
60+
i--;
61+
}
62+
}
63+
arr[i] = data;
64+
}
65+
66+
System.out.println("Array Elements : ");
67+
68+
for(int i = 0; i < arr.length; i++) {
69+
70+
System.out.print(arr[i] +" ");
71+
}
72+
System.out.println();
73+
}
74+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.io.*;
2+
3+
class InvalidCredentialsException extends RuntimeException{
4+
5+
InvalidCredentialsException(String msg) {
6+
7+
super(msg);
8+
}
9+
}
10+
11+
class Main {
12+
13+
static String userName1 = "dhirajgadekar";
14+
static String password1 = "dhiraj@123";
15+
16+
public static void main(String[] args) throws IOException {
17+
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
20+
System.out.print("Enter UserName : ");
21+
String userName2 = br.readLine();
22+
23+
System.out.print("Enter Password : ");
24+
String password2 = br.readLine();
25+
26+
27+
boolean flag = true;
28+
29+
while(flag) {
30+
if(!(userName1.equals(userName2) && password1.equals(password2))) {
31+
32+
try {
33+
34+
throw new InvalidCredentialsException("Invalid username or password");
35+
} catch(InvalidCredentialsException ivc) {
36+
37+
System.out.println(ivc.getMessage());
38+
System.out.print("Enter UserName : ");
39+
userName2 = br.readLine();
40+
41+
System.out.print("Enter Password : ");
42+
password2 = br.readLine();
43+
}
44+
} else {
45+
46+
System.out.println("Login Successful");
47+
flag = false;
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)