Exception Handling-AGD Essay

Exception HandlingWhat You Will Learn 2 Usetry,throw, catch to watch forindicate exceptionshandle How to process exceptions and failures.Motivation 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 3Overview 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 4Expectation 5678Overview 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 9Exception Handler 10 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.

Don't use plagiarized sources. Get Your Custom Essay on
Exception Handling-AGD Essay
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

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 11Terminology Throw point ” initial point at which the exception occurs, top row of call chain InputMismatchException ” occurs when Scanner method nextInt receives a string that does not represent a valid integer 12Termination Model of Exception Handling When an exception occurs: ” try block terminates immediately ” Program control transfers to first matching catch block try statement ” consists of try block and corresponding catch and/or finally blocks 13Contrast Termination model ” program control does not return to the throw point ” try block has expired; ” Flow of control proceeds to the first statement after the last catch block Resumption model ” program control resumes just after throw point 14Enclosing 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 { } 15Using the throws Clause Appears after method’s parameter list and before the method’s body Contains a comma – separated list of exceptions 16Using the throws Clause Exceptions can be thrown by statements in method’s body by methods/functions called in method’s body Exceptions can be of types listed in throws clause or subclasses With exception handling ” program catches and handles the exception 17Sequence of Events for throw 18 Preceding step try blockthrow statement unmatched catchmatching catchunmatched catch next stepSequence of Events for No throw 19 Preceding step try blockthrow statement unmatched catchmatching catchunmatched catch next stepWhen to Use Exception Handling 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. 2021Java Exception Hierarchy All exceptions inherit either directly or indirectly from class Exception Exception classes form an inheritance hierarchy that can be extended 22Java Exception Hierarchy Class 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 Error s 23Inheritance hierarchy for class Throwable 24finally Block Consists of finally keyword followed by a block of code enclosed in curly braces Optional in a try statement If present, is placed after the last catch block 25finally Block Executes whether or not an exception is thrown in the corresponding try block or any of its corresponding catch blocks Will not execute if the application exits early from a try block via method System.exit Typically contains resource – release code 26Using 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 27Sequence of Events for finally clause 28 Preceding step try blockthrow statement unmatched catchmatching catchunmatched catch next step finallyAdvantages 29Advantages 30Thank You! 31

Still stressed from student homework?
Get quality assistance from academic writers!