Download presentation
Presentation is loading. Please wait.
Published byΕυτροπια Αξιώτης Modified over 5 years ago
1
Lecture 17 Exception Handling CSE 1322 11/30/2019
2
Exception Handling An exception is an indication of a problem that occurs during a program’s execution. Exception handling enables applications to resolve exceptions. Exception handling enables clear, robust and more fault-tolerant programs. Exception handling helps improve a program’s fault tolerance. 11/30/2019
3
Exception Handling in Real life
11/30/2019
4
Exception Handling in Real life
11/30/2019
5
Exceptions 11/30/2019
6
Exceptions Illegal operations at run time can generate an exception.
For example, we have seen these exceptions: Arithmetic Exception Null Pointer Exception Input Mismatch Exception Number Format Exception Array Index Out Of Bounds Exception 11/30/2019
7
Exception Handling Consider the following pseudocode:
Perform a task If the preceding task did not execute correctly Perform error processing Perform next task If the preceding task did not execute correctly Perform error processing … In this pseudocode, we begin by performing a task; then we test whether that task executed correctly. If not, we perform error processing. 11/30/2019
8
Exception Handling Exception handling enables programmers to remove error-handling code from the “main line” of the program’s execution. Programmers can decide to handle all exceptions, all exceptions of a certain type or all exceptions of related types. Such flexibility reduces the likelihood that errors will be overlooked. 11/30/2019
9
Exception Class Hierarchy Java
The Exception class, RuntimeException class, and their subclasses are in the java.lang package. The ExceptionClass parameter to the catch block can be any of these exceptions. The IOException class and its subclasses are in the java.io package. 11/30/2019
10
Exception Class Hierarchy Java
The Exception class, RuntimeException class, and their subclasses are in the java.lang package. The ExceptionClass parameter to the catch block can be any of these exceptions. The IOException class and its subclasses are in the java.io package. 11/30/2019
11
Exception Class Hierarchy Java
The Exception class, RuntimeException class, and their subclasses are in the java.lang package. The ExceptionClass parameter to the catch block can be any of these exceptions. The IOException class and its subclasses are in the java.io package. 11/30/2019
12
C# Exception Hierarchy
System exceptions are defined in .NET libraries and are used by the framework, while application exceptions are defined by application developers and are used by the application software. 11/30/2019
13
.NET Exception Hierarchy
11/30/2019
14
Handling Exceptions We don't want invalid user input to terminate our programs! It is better to detect the problem and re-prompt the user for the input. Java and C# allow us to intercept and handle some of these exceptions using try and catch blocks. Inside the try block, we put the code that might generate an exception. Inside catch blocks, we put the code to handle any exceptions that could be generated. 11/30/2019
15
Exception Handling When a method called in a program detects a problem, the method throws an exception. The point at which an exception occurs is called the throw point If an exception occurs in a try block, program control immediately transfers to the first catch block matching the type of the thrown exception. After the exception is handled, program control resumes after the last catch block. 11/30/2019
16
Exception Handling 11/30/2019
17
Minimum try/catch Syntax
{ // code that might generate an exception } catch( ExceptionClass exceptionObjRef ) // code to recover from the exception If an exception occurs in the try block, control jumps immediately to the catch block. No further instructions in the try block are executed. If no exceptions are generated in the try block, the catch block is not executed. 11/30/2019
18
Catch Block When an exception occurs in a try block, a corresponding catch block catches the exception and handles it. At least one catch block must immediately follow a try block. A catch block specifies an exception parameter representing the exception that the catch block can handle. Optionally, you can include a catch block that does not specify an exception type to catch all exception types. 11/30/2019
19
Catch Block catch(IO.FileNotFoundException fnfe) { handle file not found (using fnfe object) } catch(Exception e) { handle other type of exception (using e object) } 11/30/2019
20
finally Block Programs frequently request and release resources dynamically. Operating systems typically prevent more than one program from manipulating a file. Therefore, the program should close the file (i.e., release the resource) so other programs can use it. If the file is not closed, a resource leak occurs. The finally block is guaranteed to execute regardless of whether an exception occurs. 11/30/2019
21
finally Block Local variables in a try block cannot be accessed in the corresponding finally block, so variables that must be accessed in both should be declared before the try block. 11/30/2019
22
Full try/catch/finally Syntax
try { // code that might generate an exception } catch( Exception1Class e1 ) // code to handle an Exception1Class exception } … catch( ExceptionNClass eN ) // code to handle an ExceptionNClass exception finally // code to execute whether or not an exception occurs 11/30/2019
23
Exception Handling in Real life
11/30/2019
24
Reminder Do not place try blocks around every statement that might throw an exception. It’s better to place one try block around a significant portion of code, and follow this try block with catch blocks that handle each possible exception. Then follow the catch blocks with a single finally block. Separate try blocks should be used when it is important to distinguish between multiple statements that can throw the same exception type. 11/30/2019
25
Checked and Unchecked Exceptions
Java distinguishes between two types of exceptions: Unchecked exceptions are those that are subclasses of Error or RuntimeException It is not mandatory to use try and catch blocks to handle these exceptions. Checked exceptions are any other exceptions. Code that might generate a checked exception must be put inside a try block or the method must acknowledge that the exception may occur by using a throws clause in the method header. Otherwise, the compiler will generate an error. 11/30/2019
26
Checked vs Unchecked Exceptions
Unchecked exceptions are those that are subclasses of Error or RuntimeException Checked exceptions are any other exceptions. 11/30/2019
27
Checked vs Unchecked Exceptions
11/30/2019
28
Checked vs Unchecked Exceptions
Unchecked exceptions are those that are subclasses of Error or RuntimeException Checked exceptions are any other exceptions. 11/30/2019
29
Checked and Unchecked Exceptions
Java distinguishes between two types of exceptions: Unchecked exceptions are those that are subclasses of Error or RuntimeException It is not mandatory to use try and catch blocks to handle these exceptions. Checked exceptions are any other exceptions. Code that might generate a checked exception must be put inside a try block or the method must acknowledge that the exception may occur by using a throws clause in the method header. Otherwise, the compiler will generate an error. 11/30/2019
30
SOFTWARE ENGINEERING TIP
Write code to catch and handle exceptions generated by invalid user input. Always try to write code that is user-friendly. Although the methods of the Exception class are good debugging tools, they are not necessarily appropriate to use in the final version of a program. 11/30/2019
31
Designing Your Own Execution Types
public class InsufficientFundsException extends RuntimeException { public InsufficientFundsException() {} public InsufficientFundsException(String message) { super(message); } } 11/30/2019
32
Throwing an Exception The pattern for a method that throws a user-defined exception is: accessModifier returnType methodName( parameters ) throws ExceptionName { if ( parameter list is legal ) process the parameter list else throw new ExceptionName( "Message here" ); } The message passed to the constructor identifies the error detected. In a client's catch block, the getMessage method retrieves that message. 11/30/2019
33
Designing Your Own Execution Types
public class _1322_Exception : Exception { public _1322_Exception(string message) : base(message) } 11/30/2019
34
Throwing an Exception The pattern for a method that throws a user-defined exception is: accessModifier returnType methodName( parameters ) { if ( parameter list is legal ) process the parameter list else throw new ExceptionName( "Message here" ); } The message passed to the constructor identifies the error detected. In a client's catch block, the Message property retrieves that message. 11/30/2019
35
Throwing an Exception { CreateExc(); } catch (_1302_Exception myE)
try { CreateExc(); } catch (_1302_Exception myE) Console.WriteLine("caught it " + myE.Message); private static void CreateExc() Console.Write("Enter 1 to see our own exception work: "); int s = int.Parse(Console.ReadLine()); if (s == 1) _1302_Exception ex = new _1302_Exception("value == 1 for testing purposes"); throw ex; 11/30/2019
36
The throw vs throws Keywords in Java
An Interview Question The throw vs throws Keywords in Java 11/30/2019
37
The throw vs throws Keywords in Java
11/30/2019
38
The final, finally, and finalize keywords in Java
Other Interview Questions The final, finally, and finalize keywords in Java 11/30/2019
39
The final, finally, and finalize keywords in Java
11/30/2019
40
The final, finally, and finalize keywords in Java
11/30/2019
41
Exception Handling in Real life
11/30/2019
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.