Skip to content

Commit d9a7479

Browse files
committed
Exceptions
1 parent 7ea4d15 commit d9a7479

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed

src/exceptions/BankAccount.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package exceptions;
2+
3+
public class BankAccount {
4+
private double balance;
5+
6+
public BankAccount(double amount) {
7+
this.balance = amount;
8+
}
9+
10+
public void withdraw(double amount) throws InsufficientFundsException {
11+
if(amount > balance) {
12+
throw new InsufficientFundsException(amount);
13+
}
14+
balance -= amount;
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package exceptions;
2+
3+
public class InsufficientFundsException extends Exception {
4+
5+
private double amount;
6+
7+
public InsufficientFundsException (double amount) {
8+
super("What do you want ? You don't have money.");
9+
this.amount = amount;
10+
}
11+
12+
public double getAmount() {
13+
return amount;
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package exceptions;
2+
3+
public class LearnCustomExceptions {
4+
public static void main(String[] args) {
5+
BankAccount bankAccount = new BankAccount(10);
6+
try {
7+
bankAccount.withdraw(11);
8+
} catch (InsufficientFundsException e) {
9+
System.out.println(e);
10+
}
11+
}
12+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package exceptions;
2+
3+
// Types of errors
4+
// - Syntax error, Logical error, Runtime error
5+
6+
// Program will crash during run time errors
7+
// so we will handle those exceptions
8+
9+
// Exception handling is a way to handle the runtime errors so that
10+
// normal flow of application can be maintained
11+
12+
// Exception is an event that disrupts the normal flow of program,
13+
// It is an object which is thrown at runtime
14+
15+
public class LearnExceptions {
16+
public static void main(String[] args) {
17+
int numerators[] = {10, 200, 30, 40};
18+
int denominators[] = {1, 2, 0, 4};
19+
for(int i = 0; i < 10; i++) {
20+
try {
21+
System.out.println(divide(numerators[i], denominators[i]));
22+
} catch (Exception e) {
23+
System.out.println(e);
24+
}
25+
}
26+
System.out.println("Good job :)");
27+
}
28+
29+
public static int divide(int a, int b) {
30+
try {
31+
return a/b;
32+
} catch (ArithmeticException e) {
33+
System.out.println("Arithmetic exception :(");
34+
return -1;
35+
} catch (Exception e) {
36+
System.out.println(e);
37+
return -1;
38+
}
39+
}
40+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package exceptions;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
class Student {
8+
int id;
9+
10+
void setId (int id) {
11+
this.id = id;
12+
}
13+
}
14+
15+
public class LearnStackTrace {
16+
public static void main(String[] args) throws IOException {
17+
try {
18+
level1();
19+
} catch (Exception o) {
20+
// StackTraceElement stackTrace[] = o.getStackTrace();
21+
// for(StackTraceElement st: stackTrace) {
22+
// System.out.println(st);
23+
// }
24+
25+
o.printStackTrace();
26+
}
27+
28+
// null-pointer exception
29+
// unchecked exception -> these exceptions are not checked at compile time
30+
Student a = null;
31+
a.setId(123);
32+
33+
34+
method2();
35+
System.out.println("Hello");
36+
}
37+
38+
public static void method2() throws FileNotFoundException {
39+
method1();
40+
}
41+
42+
public static void method1() throws FileNotFoundException {
43+
// checked exception -> checked at compile time
44+
// either handle this using try catch
45+
// or make it's caller responsible, write "throws Exception" in method in which its present
46+
// FileReader fileReader = new FileReader("a.txt");
47+
48+
try {
49+
FileReader fileReader = new FileReader("a.txt");
50+
} catch (FileNotFoundException e) {
51+
System.out.println("File not found");
52+
throw new FileNotFoundException("oops!");
53+
}
54+
}
55+
56+
// not needed to do that throws etc. here since its unchecked exception
57+
public static void someMethod() {
58+
throw new ArithmeticException();
59+
}
60+
61+
public static void level3() {
62+
int[] array = new int[5];
63+
array[5] = 10;
64+
}
65+
66+
public static void level2() {
67+
level3();
68+
}
69+
70+
public static void level1() {
71+
level2();
72+
}
73+
74+
public static int divide (int a, int b) {
75+
try {
76+
return a/b;
77+
} catch (Exception e) {
78+
return -1;
79+
}
80+
finally {
81+
// this will execute, no matter try or catch which is executing
82+
// we can print even when return statements are there in try/catch
83+
System.out.println("Bye");
84+
}
85+
// System.out.println("Bye");
86+
}
87+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package exceptions;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
// BufferedReader is using system resources which needs to be closed
8+
// to make sure its closed, we can use finally block
9+
10+
public class LearningTryWith {
11+
public static void main(String[] args) {
12+
// BufferedReader reader = null;
13+
// try {
14+
// reader = new BufferedReader(new FileReader("example.txt"));
15+
16+
// object which will be created inside the parenthesis, will get auto closed
17+
// - only the objects of class which is implementing AutoClosable interface
18+
try (BufferedReader reader = new BufferedReader(new FileReader("example.txt"))) {
19+
String line;
20+
while((line = reader.readLine()) != null) {
21+
System.out.println(line);
22+
}
23+
} catch (IOException e) {
24+
System.out.println("IOException caught: " + e.getMessage());
25+
}
26+
// finally {
27+
// try {
28+
// if (reader != null) {
29+
// reader.close();
30+
// }
31+
// } catch (IOException e) {
32+
// System.out.println("Error closing reader: " + e.getMessage());
33+
// }
34+
// }
35+
}
36+
}

0 commit comments

Comments
 (0)