cse425 assignment
cse425 assignment
CSE 425
Section 10
Abstract:
Exception and event handling are essential concepts in programming languages, allowing
developers to create robust, maintainable, and error-resistant applications. Programming
languages offer mechanisms that deal with errors and asynchronous events in a systematic
manner, promoting application reliability and responsiveness. Accordingly, the article
examines the evolution, design, and implementation of exception/event handling mechanisms
in programming languages adopting an array of programming paradigms. This paper aims to
discuss some of the important paradigms and compare the approaches towards the
implementation of handlers. Real-world examples and scenarios highlight the necessity of
these features for enhancing software quality and user experience.
Introduction:
Software systems today have become complex enough that robust mechanisms for handling a
variety of unexpected situations are needed to be put into use by the ease of which programs
are executed. Software systems have some inherent error conditions like invalid user inputs,
hardware failures, network disruptions, etc. Moreover, asynchronous events, such as sensor
data updates or user interactions, also require mechanisms for providing responsive and flexible
program behavior. To conquer this issue, most of the programming languages come with a
construct known as Exception and Event Handling.
Exception handling:
first introduced in languages such as PL/I and ADA and in fact has become a standard feature
in many modern-day programming languages like Java, Python and C++. It allows you to
manage runtime errors in a structured way by separating out the error-handling logic from the
normal program flow. Another case is event handling, which allows applications to respond
dynamically in real time, so they can react to various external stimuli, such as events generated
via graphical user interfaces (GUIs) or hardware input devices.
The paper investigates the theoretical principles, historical development, and practical
applications of exception and event handling. The survey explores critical design choices, such
as control flow constructs, language features, and inter-language interaction. It also includes
real-world examples and diagrams that illustrate how these mechanisms interact to influence
software reliability and usability.
In C++ and Java, this is called Exception handling. It helps keep software systems stable. It
promotes code readability, maintainability, and robustness — with a separation of error-
handling code from the primary logic.
int main() {
try {
divide(10, 0);
} catch (const std::runtime_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
Example:
public class ExceptionExample {
public static void divide(int a, int b) throws ArithmeticException {
if (b == 0) {
throw new ArithmeticException("Division by zero");
}
System.out.println("Result: " + (a / b));
}
To sum up, C++ vs Java exception handling both offer powerful exception handling
mechanisms that cater to their design philosophies. The key point about each language will
give developers an insight about how to use these languages in real example.
# Example usage
divide(10, 0)
divide(10, 2)
Ruby Exception Handling:
Basically, Ruby’s exception handling mechanism is using begin, rescue, else, and ensure
blocks, analogous to Python’s exception handling structure. In ruby exceptions are object
objects that inherits from the class called StandardError and developers can create there own
exception classes for handling specific errors.
Example:
def divide(a, b)
begin
result = a / b
rescue ZeroDivisionError => e
puts "Error: #{e.message}"
else
puts "Result: #{result}"
ensure
puts "Execution completed."
end
end
# Example usage
divide(10, 0)
divide(10, 2)
Overall, both Python and Ruby provide thorough and easy-to-understand exception handling
systems that achieve a balance of simple-to-use vs flexible. These cater to the purpose of
making resilient applications more readable and elegant.
Real-World Example:
Example in Java:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Event Sources: These are event-producing objects, like buttons, sliders, text fields, etc. in a
GUI. For example: Clicking a button or pressing a key creates an event.
Event Objects: Whenever an event happens, the system wraps its details in an event object. So
the ActionEvent class handles things like letting the program know that a button was just
pressed.
Event Listeners: They can be interfaces that specify methods for handling certain kinds of
events. From here, developers implement these interfaces to specify what happens when the
specific event occurs.For instance, the ActionListener interface handles action events.
Event Handlers: These are event specific methods invoked that execute application logic
when an event is fired.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
frame.add(button);
frame.setSize(300, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
In this case, you created a button using Swing and registered an ActionListener to capture click
events on this button. When we click on the button it calls the actionPerformed method which
prints a message on the Console.
1. Delegates:
● Delegates are type-safe function pointers that specify the signature of event handler
methods.
● They underlie event handling, associating methods with events.
2. event Keyword:
○ This makes sure only the event source can fire the event delegate.
button.OnClick();
}
}
In this example, a custom Button class defines a Click event using a delegate. The event is
raised by calling the OnClick method, and an event handler is attached to respond to the event.
1. Type Safety:
○ A delegate is much like a function pointer; it provides method type safety on an
event and ensures that the event handler methods have the correct signature,
minimizing potential runtime errors.
2. Modularity:
○ The event-driven model encourages separation of concerns by decoupling
consumers from a source of events.
3. Extensibility:
○ Developers define custom events and handlers, tailoring the model to meet the
varying requirements of different applications.
Conclusion:
Final Thoughts Exception and event handling are crucial components of contemporary
programming languages, empowering developers to create resilient, maintainable, and
interactive applications. Handling runtime errors in C++, Java, Python, and Ruby or providing
responsive UIs in Java and C#, show that structured approaches to error and event management
are all handled with these mechanisms. Developers are able to construct software systems that
are reliable and adaptable to user demands and real-world contexts, by taking advantage of the
strengths of each language.
Citations
● Goodenough, J. B. (1975). Exception handling: Issues and a proposed notation.
Communications of the ACM, 18(12), 683-696.
● Buhr, R. J. A., & Harji, Y. M. (1996). Event-based programming systems. IEEE
Transactions on Software Engineering, 22(9), 603-616.
● Stroustrup, B. (2000). Exception safety in C++: A pragmatic look at code. ACM
SIGPLAN Notices, 35(5), 141-150.
● Reenskaug, T. (1979). Models-Views-Controllers. Xerox PARC Memo.
● Geary, D. M., & Horstmann, C. S. (2007). Core Java, Volume II - Advanced Features.
Pearson.
● Schildt, H. (2018). Java: The Complete Reference. McGraw-Hill Education.
● Sun Microsystems. (1997). Java AWT Reference. Sun Microsystems Documentation.
● Oracle. (2024). Java Swing Tutorial. Retrieved from Oracle Java Documentation.