Documente Academic
Documente Profesional
Documente Cultură
obiect
Curs 9 – exceptii, fire de executie
J
J Acest curs
› Tratarea excepțiilor
› Controlul firelor de execuție
J Definiție
› O excepție este un eveniment “excepțional” care se produce în
timpul rulării unui program și care întrerupe execuția normală a unui
program.
› Ex:
– nu s-a putut deschide fișierul de lucru
– s-a depășit dimensiunea tabloului de elemente
› Tipuri de excepții
J Java Built-in Exceptions - examples
Arithmetic Exception
It is thrown when an exceptional condition has occurred in an arithmetic operation.
ArrayIndexOutOfBoundException
It is thrown to indicate that an array has been accessed with an illegal index. The index is either negative or
greater than or equal to the size of the array.
ClassNotFoundException
This Exception is raised when we try to access a class whose definition is not found
FileNotFoundException
This Exception is raised when a file is not accessible or does not open.
IOException
It is thrown when an input-output operation failed or interrupted
InterruptedException
It is thrown when a thread is waiting , sleeping , or doing some processing , and it is interrupted.
NoSuchFieldException
It is thrown when a class does not contain the field (or variable) specified
NoSuchMethodException
It is thrown when accessing a method which is not found.
NullPointerException
This exception is raised when referring to the members of a null object. Null represents nothing
NumberFormatException
This exception is raised when a method could not convert a string into a numeric format.
RuntimeException
This represents any exception which occurs during runtime.
StringIndexOutOfBoundsException
It is thrown by String class methods to indicate that an index is either negative than the size of the string
J Raportare excepție
› Second run
y/x = 7
ArrayIndexOutOfBoundsException:java.lang.ArrayIndexOutOfBound
sException: Index 7 out of bounds for length 5
}
}
}
J
› … run y/x = 2
z[y] = 0
› … run y/x = 1
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: Index 7 out of bounds
for length 5
at HelloWorld.Main.main(Main.java:192)
› … run y/x = 2
Exceptii netratate
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 7 out of bounds for length 5
at HelloWorld.Main.main(Main.java:192)
› … run y/x = 0
z[y] = 0
Exceptii netratate
InvalidAgeException(String s){
super(s);
}
}
class TestCustomException {
try{
TestCustomException.validate(13);
}
catch(Exception m){
System.out.println("Exception occured: "+m);
}
}
}
void start () Pornește thread-ul și metoda run() este invocată de către JVM
f.start();
f.isAlive(); // retuneaza true (starea este Runnable)
f.executie = false;
f.isAlive(); // retuneaza false (starea este Dead)
J .yield() – lasă alte thread-uri să se execute
public void run() {
for (int i = 1; i <= N; i++) {
System.out.print(" " + i);
f.yield();
}
}
}
}