

























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An in-depth explanation of java exceptions, their types, and how to handle them using try, catch, and finally blocks. It covers checked and unchecked exceptions, error types, and best practices for exception handling. It also discusses common anti-patterns and performance implications.
Typology: Essays (university)
1 / 33
This page cannot be seen from the preview
Don't miss anything!
(^) An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.
(^) Separating Error-Handling Code from "Regular" Code (^) Propagating Errors Up the Call Stack (^) Grouping and Differentiating Error Types
(^) No need to declare the exception in method’s signature (^) No compile time checking (^) Usually indicate 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 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
(^) 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 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
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()); }