24/7 writing help on your phone
Save to my list
Remove from my list
We seek robust (tough) programs. When something unexpected occurs" Ensure program detects the problem " Then program must do something about it Extensive testing of special situations can result in " spaghetti code Spaghetti code is a pejorative phrase for source code that has a complex and tangled control structure, especially one using many GOTO statements, exceptions, threads, or other "unstructured" branching constructs. It is named such because program flow is conceptually like a bowl of spaghetti , i.e. twisted and tangled.
Need mechanism to check for problem where it could occur When condition does occur" Have control passed to code to handle the problem.
Overview Exception" Indication of problem during execution Uses of exception handling" Process exceptions from program components " Handle exceptions in a uniform manner in large projects " Remove error -handling code from main line of execution A method detects an error and throws an exception" Exception handler processes the error " Uncaught exceptions yield adverse effects Might terminate program execution.
Code that could generate errors put in try blocks " Code for error handling enclosed in a catch clause " The finally clause always executes Termination of exception handling" The block in which the exception occurs expires throws clause specifies exceptions method throws
Exception "thrown" here Exception handlerException handler Thrown exception matched against first set of exception handlers If it fails to match, it is matched against next set of handlers, etc. If exception matches none of handlers, program is abandonedTerminology Thrown exception " an exception that has occurred Stack trace " Name of the exception in a descriptive message that indicates the problem " Complete method -called stack ArithmeticException " can arise from a number of different problems in arithmetic
Enclosing Code in a try Block try block " encloses code that might throw an exception and the code that should not execute if an exception occurs Consists of keyword try followed by a block of code enclosed in curly braces { }e.
g. try { }
Preceding step try blockthrow statement unmatched catchmatching catchunmatched catch next step
Preceding step try blockthrow statement unmatched catchmatching catchunmatched catch next step
Exception handling designed to process synchronous errors " Synchronous errors " occur when a statement executes " Asynchronous errors " occur in parallel with and independent of the program's flow of control Avoid using exception handling as an alternate form of flow of control.
All exceptions inherit either directly or indirectly from class
Exception classes form an inheritance hierarchy that can be extended
Throwable , superclass of Exception " Only Throwable objects can be used with the exception -handling mechanism Has two subclasses: Exception and Error " Class Exception and its subclasses represent exception situations that can occur in a Java program and that can be caught by the application " Class Error and its subclasses represent abnormal situations that could happen in the JVM " it is usually not possible for a program to recover from Errors
Using finally Suggestion" Do not use a try block for every individual statement which may cause a problem " Enclose groups of statements " Follow by multiple catch blocks
Preceding step try blockthrow statement unmatched catchmatching catchunmatched catch next step finally
Advantages
Thank You!
Exceptions and Failures in Java Programming Language. (2019, Aug 20). Retrieved from https://studymoose.com/exceptions-and-failures-in-java-programming-language-essay
👋 Hi! I’m your smart assistant Amy!
Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.
get help with your assignment