ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Errors And Exception
Errors
• Errors are classified into two categories:-
Compile-time errors:- All syntax errors will be
detected and displayed by the java compiler
and therefore these errors are known as
compile time errors
• Run-time errors :- Run time errors are type of
errors which are occurred by dividing an
integer by zero, trying to cast an instance of a
class to one of its sub class.
• Error occurred in execution time, Abnormal
termination of program, Wrong execution
result Provide an exception handling
mechanism in language system Improve the
reliability of application program that allows
simple program code for exeception check
and handling into source
Exception Definition
• Treat exception as an object
• All exceptions are instances of a class extended
from Throwable class or its subclass.
• Generally, a programmer makes new exception
class to extend the Exception class which is
subclass of Throwable class.
System-Defined Exception
• Raised implicitly by system because of illegal
execution of program
• When cannot continue program execution any
more
• Created by Java System automatically
• Exception extended from Error class
and RuntimeException class
System-Defined Exception
• IndexOutOfBoundsException :
– When beyond the bound of index in the object which use
index, such as array, string, and vector
• ArrayStoreException :
– When assign object of incorrect type to element of array
• NegativeArraySizeException :
– When using a negative size of array
• NullPointerException :
– When refer to object as a null pointer
• SecurityException :
– When violate security. Caused by security manager
• IllegalMonitorStateException :
– When the thread which is not owner of monitor involves
wait or notify method
Programmer-Defined Exception
Exceptions raised by programmer
• Check by compiler whether the exception
handler for exception occurred exists or not
– If there is no handler, it is error
• Sub class of Exception class
Exception Occurrence
• Raised implicitly by system
• Raised explicitly by programmer
– throw Statement
Exception Occurrence
class ThrowStatement extends Exception {
public static void exp(int ptr) {
if (ptr == 0)
throw new NullPointerException();
}
public static void main(String[] args) {
int i = 0;
ThrowStatement.exp(i);
}
}
class ThrowStatement extends Exception {
public static void exp(int ptr) {
if (ptr == 0)
throw new NullPointerException();
}
public static void main(String[] args) {
int i = 0;
ThrowStatement.exp(i);
}
}
java.lang.NullPointerException
at ThrowStatement.exp(ThrowStatement.java:4)
at ThrowStatement.main(ThrowStatement.java:8)
java.lang.NullPointerException
at ThrowStatement.exp(ThrowStatement.java:4)
at ThrowStatement.main(ThrowStatement.java:8)
Exception Occurrence
• throws Statement
– When programmer-defined exception is raised, if
there is no exception handler, need to describe it
in the declaration part of method
 [ThrowsClause.java]
[modifiers] returntype methodName(params) throws e1, ... ,ek {[modifiers] returntype methodName(params) throws e1, ... ,ek { }
Exception Handling
• try-catch-finally Statement
– Check and Handle the Exception
 [ExceptionHandler.java]
try {
// …
} catch (ExceptionType1 identifier) {
// …
} catch (ExceptionType2 identifier) {
// …
} finally {
// …
}
try {
// …
} catch (ExceptionType1 identifier) {
// …
} catch (ExceptionType2 identifier) {
// …
} finally {
// …
}
Exception Handling
• Default Exception Handler
– When system-defined exception occurred, if
programmer does not deal with it, it would be
processed by default exception handler
– Simple function to output error message and exit
• Execution Order of Exception Handler
– Finally clause is executed independent of
exception and catch
 [SystemHandler.java] ,
[FinallyClause.java]

More Related Content

Errors and exception

  • 2. Errors • Errors are classified into two categories:- Compile-time errors:- All syntax errors will be detected and displayed by the java compiler and therefore these errors are known as compile time errors • Run-time errors :- Run time errors are type of errors which are occurred by dividing an integer by zero, trying to cast an instance of a class to one of its sub class.
  • 3. • Error occurred in execution time, Abnormal termination of program, Wrong execution result Provide an exception handling mechanism in language system Improve the reliability of application program that allows simple program code for exeception check and handling into source
  • 4. Exception Definition • Treat exception as an object • All exceptions are instances of a class extended from Throwable class or its subclass. • Generally, a programmer makes new exception class to extend the Exception class which is subclass of Throwable class.
  • 5. System-Defined Exception • Raised implicitly by system because of illegal execution of program • When cannot continue program execution any more • Created by Java System automatically • Exception extended from Error class and RuntimeException class
  • 6. System-Defined Exception • IndexOutOfBoundsException : – When beyond the bound of index in the object which use index, such as array, string, and vector • ArrayStoreException : – When assign object of incorrect type to element of array • NegativeArraySizeException : – When using a negative size of array • NullPointerException : – When refer to object as a null pointer • SecurityException : – When violate security. Caused by security manager • IllegalMonitorStateException : – When the thread which is not owner of monitor involves wait or notify method
  • 7. Programmer-Defined Exception Exceptions raised by programmer • Check by compiler whether the exception handler for exception occurred exists or not – If there is no handler, it is error • Sub class of Exception class
  • 8. Exception Occurrence • Raised implicitly by system • Raised explicitly by programmer – throw Statement
  • 9. Exception Occurrence class ThrowStatement extends Exception { public static void exp(int ptr) { if (ptr == 0) throw new NullPointerException(); } public static void main(String[] args) { int i = 0; ThrowStatement.exp(i); } } class ThrowStatement extends Exception { public static void exp(int ptr) { if (ptr == 0) throw new NullPointerException(); } public static void main(String[] args) { int i = 0; ThrowStatement.exp(i); } } java.lang.NullPointerException at ThrowStatement.exp(ThrowStatement.java:4) at ThrowStatement.main(ThrowStatement.java:8) java.lang.NullPointerException at ThrowStatement.exp(ThrowStatement.java:4) at ThrowStatement.main(ThrowStatement.java:8)
  • 10. Exception Occurrence • throws Statement – When programmer-defined exception is raised, if there is no exception handler, need to describe it in the declaration part of method  [ThrowsClause.java] [modifiers] returntype methodName(params) throws e1, ... ,ek {[modifiers] returntype methodName(params) throws e1, ... ,ek { }
  • 11. Exception Handling • try-catch-finally Statement – Check and Handle the Exception  [ExceptionHandler.java] try { // … } catch (ExceptionType1 identifier) { // … } catch (ExceptionType2 identifier) { // … } finally { // … } try { // … } catch (ExceptionType1 identifier) { // … } catch (ExceptionType2 identifier) { // … } finally { // … }
  • 12. Exception Handling • Default Exception Handler – When system-defined exception occurred, if programmer does not deal with it, it would be processed by default exception handler – Simple function to output error message and exit • Execution Order of Exception Handler – Finally clause is executed independent of exception and catch  [SystemHandler.java] , [FinallyClause.java]