exceptionhandlinginjava-140224181412-phpapp02.pptx
□ An exception is an event, which occurs during the
execution of a program, that disrupts the normal flow of
the program's instructions.
try {
//do something
} catch (ExceptionType name) {
} catch (ExceptionType name) {
} finally {
//clean up
}
□ Separating Error-Handling Code from "Regular" Code
□ Propagating Errors Up the Call Stack
□ Grouping and Differentiating Error Types
exceptionhandlinginjava-140224181412-phpapp02.pptx
□ Part of the method signature
□ Compile type checking
□ Requires the programmer to handle the exception or
declare the method as throwing exception
□ Unique to java
□ e.g. FileNotFoundException
□ No need to declare the exception in the method’s
signature
□ No compile time checking
□ Usually indicate a programming error
□ e.g. NullPointerException
□ Indicate error in the underlying JVM
□ Error are external to the application
□ Application does not usually have to deal with these
class of Exceptions
□ e.g. OutOfMemoryError
□ Exceptions indicate a broken contract
■ Precondition (e.g. file is open for read)
■ Postcondition (e.g. read a character from file)
□ Your method encounters an abnormal condition that it can't
handle
□ If your method is unable to fulfill its contract, throw
either a checked or unchecked exception.
□ Exceptions v/s Errors
■ Errors are for JVM
■ Exceptions for rest of us
□ Checked v/s Unchecked exceptions
■ Can caller recover from this error?
■ Yes: checked
■ No: unchecked
1. When you can handle the exception
2. When you need to throw a different type of exception
3. Refer to 1 & 2
□ To achieve Flow control using exception
try {
while (true) {
increaseCount();
}
} catch (MaximumCountReachedException ex) {
}
//Continue execution
}
public void increaseCount()
throws MaximumCountReachedException {
if (count >= 5000)
throw new MaximumCountReachedException();
}
□ What went wrong?
□ Where did it go wrong?
□ Why did it go wrong?
□ If your exception does not provide
answers to all these questions, you
are doing something wrong!
□ Exceptions are expensive for the JVM
□ Creating stack traces requires
resources and CPU
□ the Java VM requires more efforts to
handle a thrown exception than a
normal method
□ Log and Throw
□ Throwing Generic Exception
□ Catching Generic Exception
□ Destructive Wrapping
□ Log and Return Null
□ Catch and Ignore (a.k.a. Head in the Sand)
□ Throw from Within Finally
□ Multi-Line Log Messages
□ Unsupported Operation Returning Null
□ Log the error and throw the same
exception again
□ Messy log file
□ Achieves nothing
□ The caller does not know the nature
of error – hinders error handling
□ We are masking programming errors
public SomeInterface buildInstance(String
className) {
SomeInterface impl = null;
try {
Class clazz = Class.forName(className);
impl =
(SomeInterface)clazz.newInstance();
}
catch (Exception e) {
log.error("Error creating class: " +
className);
}
return impl;
}
catch (NoSuchMethodException e) {
throw new MyServiceException("Blah:
" +
e.getMessage());
}
catch (NoSuchMethodException e) {
LOG.error("Blah", e);
return null;
}
catch (NoSuchMethodException e) {
}
try {
blah();
} finally {
cleanUp();
}
ANTI PATTERNS - MULTI-LINE LOG
MESSAGES
LOG.debug("Using cache policy A");
LOG.debug("Using retry policy B");
public String foo() {
// Not supported in this
implementation.
return null;
}
□ Throw
UnsupportedOperationException
□
□
□
□
□
□
□
□
Throw checked exception when caller can recover
from error
Throw runtime exception when the caller cannot
recover
Throw runtime exception for programming error
Throw early, catch late
Use NestedException
Don’t catch an exception if you cant do any thing
about it.
Log exception only once, and at the latest possible
time
Default Error Page in presentation layer for all
Runtime Exceptions
try{
..some code that throws
XXXException
}catch(XXXException ex){
throw new RuntimeException(ex);
}
□ Log all internal states
□ Log all parameters to the method
that failed
□ Log all data required to trace the
error
□ Ensure log statements don’t cause
NPE*
□ Define a hierarchy of exceptions.
□ Lower level module throws lower level
exceptions, higher level module
encapsulate lower level exceptions
□ Define which exceptions will cause
transaction to rollback
Exceptions and Transactions
□ @ApplicationException(rollback=true)
public class FooException extends
Exception ...
Best practices in EJB exception handling
http://www.ibm.com/developerworks/library/j-ejbexcept.html
Beware the dangers of generic Exceptions
http://www.javaworld.com/javaworld/jw-10-2003/jw-1003-
generics.html
Exception Handling in Web Applications
http://weblogs.java.net/blog/crazybob/archive/2004/02/exception_han
dl.html
Designing with Exceptions
http://www.artima.com/designtechniques/desexceptP.html
Build a better exception-handling framework
http://www.ibm.com/developerworks/java/library/j-ejb01283.html
JAVA EXCEPTIONS
http://www.javaolympus.com/J2SE/Exceptions/JavaExceptions.jsp
Exception-Handling Antipatterns
http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html
Three Rules for Effective Exception Handling
http://today.java.net/pub/a/today/2003/12/04/exceptions.html
13 Exceptional Exception Handling Techniques
http://www.manageability.org/blog/stuff/exceptional-exception-handling-techniques
Best Practices for Exception Handling
http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html
Lesson: Exceptions
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

More Related Content

PPT
Exception handling in java
PPTX
Introduction to java exceptions
PPT
Exception handling
PPTX
Training material exceptions v1
PPTX
Exception handling
PDF
Exception Handling.pdf
DOCX
Java Exception handling
PDF
Design byexceptions
Exception handling in java
Introduction to java exceptions
Exception handling
Training material exceptions v1
Exception handling
Exception Handling.pdf
Java Exception handling
Design byexceptions

Similar to exceptionhandlinginjava-140224181412-phpapp02.pptx (20)

PPT
JP ASSIGNMENT SERIES PPT.ppt
PPT
Exception Handling
PPTX
12. Java Exceptions and error handling
PDF
Exception handling
PDF
Exception handling
PPT
Exception handling
PPSX
Java Exceptions
PPSX
Java Exceptions Handling
PPTX
Exception handling and throw and throws keyword in java.pptx
PPTX
java exception.pptx
PPT
Exception handling
PPTX
Exception Handling
PPT
Java: Exception
PPT
exceptions in java
PPTX
Exceptions in Java
DOCX
Unit5 java
PPTX
130410107010 exception handling
PPT
Exceptions
PPTX
Java exception handling
PPT
Exceptions in java
JP ASSIGNMENT SERIES PPT.ppt
Exception Handling
12. Java Exceptions and error handling
Exception handling
Exception handling
Exception handling
Java Exceptions
Java Exceptions Handling
Exception handling and throw and throws keyword in java.pptx
java exception.pptx
Exception handling
Exception Handling
Java: Exception
exceptions in java
Exceptions in Java
Unit5 java
130410107010 exception handling
Exceptions
Java exception handling
Exceptions in java
Ad

Recently uploaded (20)

PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
20th Century Theater, Methods, History.pptx
PDF
Trump Administration's workforce development strategy
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
My India Quiz Book_20210205121199924.pdf
PDF
Weekly quiz Compilation Jan -July 25.pdf
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PDF
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
PDF
IGGE1 Understanding the Self1234567891011
PDF
advance database management system book.pdf
PPTX
Introduction to pro and eukaryotes and differences.pptx
PPTX
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
PDF
Empowerment Technology for Senior High School Guide
A powerpoint presentation on the Revised K-10 Science Shaping Paper
Share_Module_2_Power_conflict_and_negotiation.pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
20th Century Theater, Methods, History.pptx
Trump Administration's workforce development strategy
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
My India Quiz Book_20210205121199924.pdf
Weekly quiz Compilation Jan -July 25.pdf
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
MBA _Common_ 2nd year Syllabus _2021-22_.pdf
TNA_Presentation-1-Final(SAVE)) (1).pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 2).pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
IGGE1 Understanding the Self1234567891011
advance database management system book.pdf
Introduction to pro and eukaryotes and differences.pptx
Onco Emergencies - Spinal cord compression Superior vena cava syndrome Febr...
Empowerment Technology for Senior High School Guide
Ad

exceptionhandlinginjava-140224181412-phpapp02.pptx

  • 2. □ An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
  • 3. try { //do something } catch (ExceptionType name) { } catch (ExceptionType name) { } finally { //clean up }
  • 4. □ Separating Error-Handling Code from "Regular" Code □ Propagating Errors Up the Call Stack □ Grouping and Differentiating Error Types
  • 6. □ Part of the method signature □ Compile type checking □ Requires the programmer to handle the exception or declare the method as throwing exception □ Unique to java □ e.g. FileNotFoundException
  • 7. □ No need to declare the exception in the method’s signature □ No compile time checking □ Usually indicate a programming error □ e.g. NullPointerException
  • 8. □ Indicate error in the underlying JVM □ Error are external to the application □ Application does not usually have to deal with these class of Exceptions □ e.g. OutOfMemoryError
  • 9. □ Exceptions indicate a broken contract ■ Precondition (e.g. file is open for read) ■ Postcondition (e.g. read a character from file) □ Your method encounters an abnormal condition that it can't handle □ If your method is unable to fulfill its contract, throw either a checked or unchecked exception.
  • 10. □ Exceptions v/s Errors ■ Errors are for JVM ■ Exceptions for rest of us □ Checked v/s Unchecked exceptions ■ Can caller recover from this error? ■ Yes: checked ■ No: unchecked
  • 11. 1. When you can handle the exception 2. When you need to throw a different type of exception 3. Refer to 1 & 2
  • 12. □ To achieve Flow control using exception try { while (true) { increaseCount(); } } catch (MaximumCountReachedException ex) { } //Continue execution } public void increaseCount() throws MaximumCountReachedException { if (count >= 5000) throw new MaximumCountReachedException(); }
  • 13. □ What went wrong? □ Where did it go wrong? □ Why did it go wrong? □ If your exception does not provide answers to all these questions, you are doing something wrong!
  • 14. □ Exceptions are expensive for the JVM □ Creating stack traces requires resources and CPU □ the Java VM requires more efforts to handle a thrown exception than a normal method
  • 15. □ Log and Throw □ Throwing Generic Exception □ Catching Generic Exception □ Destructive Wrapping □ Log and Return Null □ Catch and Ignore (a.k.a. Head in the Sand) □ Throw from Within Finally □ Multi-Line Log Messages □ Unsupported Operation Returning Null
  • 16. □ Log the error and throw the same exception again □ Messy log file □ Achieves nothing
  • 17. □ The caller does not know the nature of error – hinders error handling
  • 18. □ We are masking programming errors
  • 19. public SomeInterface buildInstance(String className) { SomeInterface impl = null; try { Class clazz = Class.forName(className); impl = (SomeInterface)clazz.newInstance(); } catch (Exception e) { log.error("Error creating class: " + className); } return impl; }
  • 20. catch (NoSuchMethodException e) { throw new MyServiceException("Blah: " + e.getMessage()); }
  • 21. catch (NoSuchMethodException e) { LOG.error("Blah", e); return null; }
  • 23. try { blah(); } finally { cleanUp(); }
  • 24. ANTI PATTERNS - MULTI-LINE LOG MESSAGES LOG.debug("Using cache policy A"); LOG.debug("Using retry policy B");
  • 25. public String foo() { // Not supported in this implementation. return null; } □ Throw UnsupportedOperationException
  • 26. □ □ □ □ □ □ □ □ Throw checked exception when caller can recover from error Throw runtime exception when the caller cannot recover Throw runtime exception for programming error Throw early, catch late Use NestedException Don’t catch an exception if you cant do any thing about it. Log exception only once, and at the latest possible time Default Error Page in presentation layer for all Runtime Exceptions
  • 27. try{ ..some code that throws XXXException }catch(XXXException ex){ throw new RuntimeException(ex); }
  • 28. □ Log all internal states □ Log all parameters to the method that failed □ Log all data required to trace the error □ Ensure log statements don’t cause NPE*
  • 29. □ Define a hierarchy of exceptions. □ Lower level module throws lower level exceptions, higher level module encapsulate lower level exceptions □ Define which exceptions will cause transaction to rollback
  • 30. Exceptions and Transactions □ @ApplicationException(rollback=true) public class FooException extends Exception ...
  • 31. Best practices in EJB exception handling http://www.ibm.com/developerworks/library/j-ejbexcept.html Beware the dangers of generic Exceptions http://www.javaworld.com/javaworld/jw-10-2003/jw-1003- generics.html Exception Handling in Web Applications http://weblogs.java.net/blog/crazybob/archive/2004/02/exception_han dl.html Designing with Exceptions http://www.artima.com/designtechniques/desexceptP.html Build a better exception-handling framework http://www.ibm.com/developerworks/java/library/j-ejb01283.html
  • 32. JAVA EXCEPTIONS http://www.javaolympus.com/J2SE/Exceptions/JavaExceptions.jsp Exception-Handling Antipatterns http://today.java.net/pub/a/today/2006/04/06/exception-handling-antipatterns.html Three Rules for Effective Exception Handling http://today.java.net/pub/a/today/2003/12/04/exceptions.html 13 Exceptional Exception Handling Techniques http://www.manageability.org/blog/stuff/exceptional-exception-handling-techniques Best Practices for Exception Handling http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html Lesson: Exceptions http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html