#9 error-handling + exceptions + generics Flashcards
(9 cards)
how to exit the program if an error occurred
if(condition indicating error){
system.err.println (“Goodbye Cruel JVM”);
system.exit(-1);
}
error handling with exceptions
(you don’t have to throw an exception, you could just do if (exception occurred){
return whatever code you please;}
void method1(){ if(exception occurred) throw new ExceptionType(); }
void method2(){ try{ method1(); } catch (ExceptionType e) { } }
exceptions are objects derived from which class?
throwable
Throwable (); //no error message
Throwable(String msg); // with error message
String getMessage (); //return error message
void printStackTrace(); //record methods called, and location
two types of errors
checked errors: must be handled by the programmer
unchecked errors: serious errors that a typical program should not be required to handle
IO Exception(java.io)
used for generic I/O errors
IllegalArgumentException(java.lang)
if method has passed in an illegal or inappropriate argument
NullPointerException(java.lang)
a null reference is dereferenced
ArrayIndexOutOfBoundsException(java.lang)
array subscript is invalid
UnsupportedOperationException
a called operation does not exist