-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionsDemo.java
More file actions
95 lines (77 loc) · 3.04 KB
/
Copy pathExceptionsDemo.java
File metadata and controls
95 lines (77 loc) · 3.04 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.*;
/**
* Ce sunt exceptiile? Prin exceptii intelegem faptul ca se petrece ceva prost sau nedorit in cadrul programului nostru, de exemplu impartire cu 0 sau
* accesarea unui element dintr-un array aiurea (arr[-2], arr[arr.length + 69])
* In Java avem o clasa numita Exception, care este mostenita de toate exceptiile predefinite in Java (FileNotFoundException, IndexOutOfBoundsException etc ca-s multe)
* -> acestea apar implicit
* Noi putem sa ne facem exceptii custom, prin extinderea clasei Exception
*/
class MyFirstException extends Exception {
public MyFirstException() {
super("My first exception thrown");
}
}
class MySecondException extends Exception {
public MySecondException(String name) {
super("Exception " + name + " is thrown");
}
}
class SomeStuff {
private boolean isOk = false;
public void setOk(boolean bool) {
isOk = bool;
}
// daca in aceasta metoda apare ceva ce nu ne convine sau e prost -> aruncam exceptia
public void doStuff() throws MyFirstException {
System.out.println("WORK MAN");
if (isOk)
throw new MyFirstException();
}
}
class Papa {
static {
System.out.println("Papa static");
}
{
System.out.println("Papa non - static");
}
}
class Johnny extends Papa {
static {
System.out.println("Johnny static");
}
{
System.out.println("Johnny non - static");
}
}
/**
* blocurile de initializare
* cele statice se executa doar o data, la prima instantiere a clasei, iar cele non - statice ori de cate ori e instantiata clasa respectiva
*
* la mostenire, mai intai se executa blocurile statice din clasa parinte, apoi cele statice din clasa copil
* apoi cele non statice din clasa parinte, apoi cele non statice din clasa copil
*/
public class ExceptionsDemo {
public static void main (String[] args) {
Papa papa = new Papa();
System.out.println("Kid");
Johnny johnny = new Johnny();
// incercam sa facem actiuni, in care pot aparea exceptii, in blocul try
try {
SomeStuff lab = new SomeStuff();
lab.doStuff();
lab.setOk(true);
lab.doStuff();
} catch (MyFirstException e) {
// daca s-a prins exceptia
e.printStackTrace(); // e obligatoriu sa scrii chestia asta la exceptii (se poate sa nu o scrii, dar e bad practice, caci programul nu se intrerupe, intreruperea fiind dorita)
} catch (Exception e) {
e.printStackTrace();
// putem lua in calcul ca se pot arunca mai multe tipuri de exceptii, asa putem face o constructie ca mai sus, luand exceptiile cele mai de jos ca mostenire primele
// terminand cu cele mai de sus (vorbind de mostenire)
} finally {
// blocurile din finally se executa mereu, indiferent daca s-a aruncat o exceptie sau nu
System.out.println("Leaving...");
}
}
}