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!
Remember! This is just a sample.
You can get a custom paper by one of our expert writers.
Get your custom essay
Helping students since 2015
Essay Writing Service Features
Our Experience
No matter how complex your assignment is, we can find the right professional for your specific task. Contact Essay is an essay writing company that hires only the smartest minds to help you with your projects. Our expertise allows us to provide students with high-quality academic writing, editing & proofreading services.Free Features
Free revision policy
$10Free bibliography & reference
$8Free title page
$8Free formatting
$8How Our Essay Writing Service Works
First, you will need to complete an order form. It's not difficult but, in case there is anything you find not to be clear, you may always call us so that we can guide you through it. On the order form, you will need to include some basic information concerning your order: subject, topic, number of pages, etc. We also encourage our clients to upload any relevant information or sources that will help.
Complete the order formOnce we have all the information and instructions that we need, we select the most suitable writer for your assignment. While everything seems to be clear, the writer, who has complete knowledge of the subject, may need clarification from you. It is at that point that you would receive a call or email from us.
Writer’s assignmentAs soon as the writer has finished, it will be delivered both to the website and to your email address so that you will not miss it. If your deadline is close at hand, we will place a call to you to make sure that you receive the paper on time.
Completing the order and download