Hallo Leute !
Ich habe da ein paar Fragen zu throw Exception also das grundprinzip habe ich verstanden auch habe ich es schon mehrmal angewendet nur leider funzt es gerade nicht so wie ich möchte
Ich habe hier einen kleinen rechner mit AWT-Geschrieben sieht so aus
|
Source code
|
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
package Rechner;
import java.awt.*;
import java.awt.event.*;
public class Hauptfenster extends Frame {
// Instanzvariable
private Panel eingabePanel, operatorenPanel, ergebnisPanel;
private TextField zahl1, zahl2;
private Button plus, minus, div, multi;
private Label ergebnis;
//Konstruktor
public Hauptfenster(){
super("Rechner");
//Layout für das Hauptfenster
this.setLayout(new GridLayout(4,1));
//Aufbau des eingabePanel
eingabePanel=new Panel();
eingabePanel.setLayout(new GridLayout(2,1,10,10));
zahl1= new TextField("Zahl1");
zahl2 = new TextField("Zahl2");
eingabePanel.add(zahl1);
eingabePanel.add(zahl2);
// Aufbau des OperatorenPanels
operatorenPanel= new Panel();
operatorenPanel.setLayout(new FlowLayout());
plus=new Button ("+");
minus=new Button("-");
div=new Button("/");
multi=new Button("*");
operatorenPanel.add(plus);
operatorenPanel.add(minus);
operatorenPanel.add(div);
operatorenPanel.add(multi);
//Evenets für die Knöpfe
plus.addActionListener(new KnopfAktion());
minus.addActionListener(new KnopfAktion());
div.addActionListener(new KnopfAktion());
multi.addActionListener(new KnopfAktion());
//ergebnisPanel
ergebnisPanel = new Panel();
ergebnis =new Label("Bitte Wählen");
ergebnisPanel.add(ergebnis);
// einfügen in Container
this.add(eingabePanel);
this.add(operatorenPanel);
this.add(ergebnisPanel);
//Event-Handling fürs Fenster
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent w){
System.exit(0);
}
});
}
class KnopfAktion implements ActionListener{
public void actionPerformed(ActionEvent e){
berechnen (e);
}
}
private void berechnen(ActionEvent e){
//ergebniss
double erg=0.0d;
// Umwandlung Zahl1 und Zahl2
// benötigt wrapperKlassen
// von innen nach außen arbeiten!
double z1= Double.parseDouble(zahl1.getText()) ;
double z2= Double.parseDouble(zahl2.getText());
//entscheiden welcher Knopf gedrückt
// und berechnen
//addition
if(e.getActionCommand()==plus.getLabel()){
erg=z1+z2;
}
//subtraktion
if(e.getActionCommand()==minus.getLabel()){
erg=z1-z2;
}
// mulitplikation
if(e.getActionCommand()==multi.getLabel()){
erg=z1*z2;
}
if(e.getActionCommand()==div.getLabel()){
erg=z1/z2;
}
//Ausgabe
//Wieder WrapperKlassen benötigt da erg kein String ist
// Achtung wieder von innen nach außen arbeiten !
ergebnis.setText( String.valueOf(erg) );
}
}
|
So der kann +- * / funktioniert auch super...... Jetzt könnte es aber sein das anstelle einer zahl ein buchstabe eingegeben wird nun bekommen wir ja eine NumberFormatException diese möchte ich auffagen
dashalb habe ich folgendes gemacht :
|
Source code
|
1
2
3
4
5
6
7
8
9
|
try{
double z1= Double.parseDouble(zahl1.getText()) ;
double z2= Double.parseDouble(zahl2.getText());
}catch(NumberFormatException f){
System.out.println("Bitte geben Sie eine Zahl ein!");
}
|
so nun passiert folgendes die funktion berechne (e) kann nun die z1 und z2 nicht mehr resolven .....
und die Console sagt :Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
nun habe ich mir gedacht ich fange vieleicht die Exception an der falschen stelle ab habe nun folgendes gemacht :
|
Source code
|
1
2
3
4
5
6
7
|
try{
zahl1= new TextField("Zahl1");
zahl2 = new TextField("Zahl2");
}catch(NumberFormatException f){
System.out.println("Bitte geben Sie eine Zahl ein ");
}
|
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "g"
so ist ja eigentlich klar weil das kann ja gar nicht die richtige stelle sein da ich ja hier einen String eingeben will bzw muss so
nun denk ich mal war ich am anfang schon richtig bei :
|
Source code
|
1
2
|
double z1= Double.parseDouble(zahl1.getText()) ;
double z2= Double.parseDouble(zahl2.getText());
|
aber warum kann er jetzt z1 und z2 nicht mehr resolven ?????
oder bin ich doch an der falschen stelle ? ???
habe auch schon daran gedacht z1 und z2 außerhalb des try / catch blockes zu deklariern
|
Source code
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private void berechnen(ActionEvent e){
//ergebniss
double z1;
double z2;
double erg=0.0d;
// Umwandlung Zahl1 und Zahl2
// benötigt wrapperKlassen
// von innen nach außen arbeiten!
try{
z1= Double.parseDouble(zahl1.getText()) ;
z2= Double.parseDouble(zahl2.getText());
}catch (NumberFormatException f){
System.out.println("Bitte geben Sie eine Zahl ein ");
}
|
nun sagt die Console folgendes dazu:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:
The local variable z1 may not have been initialized
The local variable z2 may not have been initialized
The local variable z1 may not have been initialized
The local variable z2 may not have been initialized
The local variable z1 may not have been initialized
also ist jetzt plötzlich nicht mehr initialisiert vorher war sie nicht mehr resolved
Wie kann ich nun die Exception abfangen bzw wo entsteht diese überhaupt ?????
Viele Grüße
the_old_pirate