0% found this document useful (0 votes)
3 views31 pages

Lecture 2

2

Uploaded by

dovanminh253
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views31 pages

Lecture 2

2

Uploaded by

dovanminh253
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

61FIT3SE1 - Software Engineering 1

Lecture 2
Exceptions
Outline
• Why exceptions?
• Java error handling mechanism
• Checked & unchecked exceptions
• Working with exceptions
• Design issues
References

• Guttag, Liskov, Program development in Java, chapter 4


Why exceptions?

• Problem: how to design robust procedures


• Robust procedures behave in a well-defined way when errors occur
• Show error and let user continue working
• Provide user with alternative options
• Requirements
• Not to terminate abruptly
• Provides an approximation of the normal behavior
Example: invalid input problem

• A behavior is required for


invalid input.

• Do you notice any problem?


Two solutions

• (1) Return a 'special' value to indicate failure


• Error is treated as part of the normal flow

• (2) Generate a special 'signal'


• Error is an exceptional case
• Separate from the normal flow
Solution 1 example
Limitations of solution 1

• Returned value does not carry error details


• Some procedures cannot have special return values
• Error code may be ignored by caller
• Additional check is required to detect an error
Solution 2: Exception

• The ‘special signal’ is called Exception


• No returned values needed
• Halt execution until error is recovered
• Caller can be forced to handle exceptions
• This makes sure the error is always handled
• Exception is a data type that carry error details
Solution 2 example
Java error handling mechanism

• Use special 'signal'


• Execution errors include:
• Environmental errors: derived from Error class
• Program errors: Exceptions
• New exception types can be created:
• Checked or unchecked
• Methods are specified with exceptions
Java Exception type hierarchy
Checked vs. unchecked exception

Checked exceptions Unchecked exceptions

If a procedure might throw a checked exception, Java requires that If a procedure might throw an unchecked exception, unchecked
the exception be listed in the procedure’s header; otherwise, there exceptions need not be listed in the header.
will be a compile-time error.

If code calls a procedure that might throw a checked exception, Java If code calls a procedure that might throw a unchecked exception,
requires that it handles the exception; otherwise, there will be a unchecked exceptions need not be handled in the calling code
compile-time error.

Checked exceptions are subtype of Exception class Unchecked exceptions are subtype of RuntimeException class
Handle an exception

• Deals with an exception in two ways:


• Surrounds the using code with try...catch
• Propagate the exception: This occurs when a call within some procedure P signals an
exception that is not handled by a catch clause of any containing try statement in P.
In this case, Java automatically propagates the exception to P’s caller provided one of the
following is true:
• that exception type or one of its supertypes is listed in P’s header
• the exception type is unchecked.
Example: try…catch
Create a new exception in Java

• Determine the exception type


• Name an exception
• Declare a new exception type
Determine the exception type
• Types of error:
• Invalid input: e.g. array is null
• Logic: e.g. division by zero
• Others: e.g. disk access failure
• You should use an unchecked exception only if you expect that users will
usually write code that ensures the exception will not happen, because
• There is a convenient and inexpensive way to avoid the exception.
• The context of use is local.
• Otherwise, you should use a checked exception.
• Reuse exceptions where possible
Create a new exception name

How to name an exception?


• Exception name = type of error + “Exception”
• Examples:
• array is null → NullPointerException
• division by zero → DivideByZeroException
• disk access failure → DiskAccessException
Declare a new exception type

• Create a subtype of Exception (if checked) or RuntimeException (if


unchecked)
• Placed in the same or a separate package (better)
• Implement constructor method(s):
• Default constructor
• Single-argument constructor: error message
• Invoke suitable super constructors
• Add new methods (if needed)
Example

public class NewKindOfException extends Exception {


public NewKindOfException( ) { super( ); }
public NewKindOfException(String s) { super(s); }
}
Working with exceptions

• Specify a procedure with exceptions


• Specify a type with exceptions
• Throw an exception in the code
• Handle an exception techniques
Specify a procedure with exceptions

• A procedure that can terminate exceptionally is indicated by having a


throws clause in its header:
throws < list_of_types >
• A procedure can throw more than one type of exception
Specify a type with exceptions

• Add suitable exceptions to these operations of a type:


• Constructor
• Setter
• Data validation (optional)
Throw an exception

• A Java procedure can terminate by throwing an exception. It does this by


using the throw statement
• Example:
Handle exception techniques
• Three techniques:
• Logging
• Masking
• Reflecting
• A combination of the above three techniques is also common
Example: Exception logging
Mask an exception

• When exceptions are expected as part of the logic


• Handle the exception and then continue with normal flow
• (if required) return an exceptional value
• Often used together with logging
Example: Masking
Reflect an exception

• Reflecting an exception can be accomplished by:


• automatic propagation
• explicitly catching an exception and then throwing an exception
Example: Delegating exception handling
Summary

• Exceptions are needed in robust programs


• Exceptions can be checked or unchecked
• Procedures/operations are specified with exceptions
• Implementations must throw the specified exceptions
• Exceptions are handled in the code by a combination of logging and
masking or reflecting

You might also like