Documente Academic
Documente Profesional
Documente Cultură
20.04.2006
20.04.2006
Programul:
import java.util.Scanner;
public class InputMismatchExceptionDemo {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter one integer:");
int inputNumber = keyboard.nextInt();
System.out.println("The square of " + inputNumber + " is " +
inputNumber * inputNumber);
}
}
Cu intrarea: Enter
D rezultatul:
one integer:h1
java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at InputMismatchExceptionDemo.main(InputMismatchExceptionDemo.java:11)
20.04.2006
20.04.2006
Excepii i erori
20.04.2006
20.04.2006
neobinuit
20.04.2006
Mechanismul try-throw-catch
20.04.2006
Mechanismul try-throw-catch
Mechanismul try-throw-catch
throw new
NumeleClaseiExceptie(PosibilArgumente);
unei excepii
20.04.2006
20.04.2006
Mechanismul try-throw-catch
catch(Exception e) {
CodDeTratareAExceptiei
}
excepiei
catch
20.04.2006
10
Mechanismul try-throw-catch
11
20.04.2006
12
Mechanismul try-throw-catch
Mechanismul try-throw-catch
catch(Exception e) { . . . }
20.04.2006
13
20.04.2006
Mechanismul try-throw-catch
14
Mechanismul try-throw-catch
20.04.2006
15
20.04.2006
16
Mechanismul try-throw-catch
17
20.04.2006
try
{
num
= Integer.
Integer.parseInt(
parseInt( inData
);
inputOK = true;
true;
}
catch (NumberFormatException
(NumberFormatException ex )
{
System.
System.out.
out.println(
println(Ati
(Ati introdus
date invalide."
invalide." );
System.
System.out.
out.println(
println(Va
(Va rog sa
reincercati.
reincercati.\n" );
}
18
Clauza finally
19
20.04.2006
20
Clauza finally
Clauza finally
Exemplu BlueJ
(ExceptFinallyEx)
20.04.2006
21
20.04.2006
try {
// Bloc de cod cu puncte de iesire multiple
}
catch (OneException e) {
System.out.println(Am interceptat OneException!");
}
catch (OtherException e) {
System.out.println(" Am interceptat OtherException!");
}
catch (AnotherException e) {
System.out.println(" Am interceptat AnotherException!");
}
finally {
// Bloc de cod executat intotdeauna la iesirea din blocul,
// indiferent de cum s-a iesit din try.
System.out.println("Finally este executat intotdeauna");
}
23
OOP8 - M. Joldo - T.U. Cluj
22
Clase excepie
20.04.2006
20.04.2006
24
20.04.2006
25
. . . // codul metodei
try
{
. . .
throw new
Exception(StringArgument);
. . .
}
catch(Exception e)
{
String message =
e.getMessage();
System.out.println(message);
System.exit(0);
} . . .
20.04.2006
programator
StringArgument este
folosit ca valoare pentru
variabila instan de tip ir
a excepiei e
26
27
20.04.2006
20.04.2006
28
29
20.04.2006
20.04.2006
31
30
S pstreze getMessage
20.04.2006
32
20.04.2006
catch (Exception e)
{ . . . }
catch (NegativeNumberException e)
{ . . . }
Deoarece NegativeNumberException este un tip de Exception,
toate NegativeNumberExceptions vor fi interceptate de ctre
primul bloc catch nainte de a ajunge vreodat la cel de-al doilea
33
20.04.2006
34
//clauza throws
35
20.04.2006
36
20.04.2006
37
20.04.2006
39
38
20.04.2006
20.04.2006
40
41
20.04.2006
20.04.2006
42
Excepii
verificate
Excepii neverificate
Excepii verificate
43
20.04.2006
44
20.04.2006
45
20.04.2006
Propagarea excepiei
Un alt exemplu
public void doFileWork(String
filename) throws
DatabaseException{
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try{
fos = new
FileOutputStream(filename);
oos = new
ObjectOutputStream(fos);
oos.writeObject(obj);
}
catch(IOException e){
throw new DatabaseException(
"Problem while working with
"+filename+": "
+e.getMessage());
}
20.04.2006
46
47
20.04.2006
finally{
try{
if(oos!=null){
oos.close();
}
if(fos!=null){
fos.close();
}
}
catch(IOException e){
throw new DatabaseException(
"Problem while working with
"+filename+": "
+e.getMessage());
}
}
}
48
20.04.2006
49
20.04.2006
20.04.2006
50
51
20.04.2006
52
Re-aruncarea excepiilor
20.04.2006
53
try {
cap(0);
} catch(ArithmeticException e) {
throw e;
}
20.04.2006
54
Re-aruncarea excepiilor
Exemplu BlueJ
(DataSetReader)
20.04.2006
55
20.04.2006
56
Rezumat
Excepii i erori
Excepii verificate i neverificate
Instruciunile try i catch
Clauza finally
Regula "prinde sau declar"
Clauza throws
Instruciunea throw
20.04.2006
57