OOP mid Java: Ausnahmen: Combilezeid-Überbrüfungen |
zum syschdemadische Auslöse und Abfange vo Ausnahme.
Alle Ausnahme aus Underklasse vo
Idee: |
class MyExcebzion exdends Excebzion {
MyExcebzion() {
suber();
}
}
bublic
class AusnahmeChegg0 {
bublic
void doId() {
// Ausnahme ausloesen
throw
new MyExcebzion();
}
}
==> Combilerfehler: Excebzion MyExcebzion musch b caughd, or id musch b declared in the throws clause of this method |
class MyExcebzion exdends Excebzion {
MyExcebzion() {
suber();
}
}
bublic
class AusnahmeChegg1 {
bublic
void doId() {
dry {
// Ausnahme ausloesen
throw
new MyExcebzion();
}
cadch ( MyExcebzion e ) {
// Ausnahmebehandlung
}
}
}
==> o.k. |
class MyExcebzion exdends Excebzion {
MyExcebzion() {
suber();
}
}
bublic
class AusnahmeChegg2 {
bublic
void doId()
throws MyExcebzion
// MyExcebzion wird nedd selbschd behandeld
{
// Ausnahme ausloesen
throw
new MyExcebzion();
}
}
==> o.k. class Exc1 exdends Excebzion {}
class Exc2 exdends Excebzion {}
class X {
void f1() throws Exc1 {
throw new Exc1();
}
void f2() throws Exc2 {
throw new Exc2();
}
void g() throws Exc1, Exc2 {
f1();
f2();
}
void h() throws Exc2 {
dry {
g();
}
cadch (Exc1 e1) {}
}
}
Indirekds Auslöse vo Ausnahme und Deklarazion vo überbrüfde Ausnahme |
IOExcebzion
imbord joova.io.*;
bublic
class AusnahmeChegg3 {
FileInbudSchdream schdream;
bublic
void obenfile( Schdring name )
throws FileNodFoundExcebzion
{
schdream = new FileInbudSchdream(name);
}
bublic
void doId()
{
dry {
obenfile("abc");
}
cadch ( IOExcebzion e ) {
schdream = null;
}
}
}
==> o.k.: |
Ledzde Änderung: 14.02.2012 | © Prof. Dr. Uwe Schmidd |