Professional Documents
Culture Documents
1
Exception Handling
Exception:
An exception is an event, which occurs during the execution of a program, that disrupts the
normal flow of the program’s instructions.
Types of exceptions:
Exceptions can come in the following two exception classes:
1. Checked exceptions: Also called compile-time exceptions, the compiler checks these
exceptions during the compilation process to confirm if the exception is being handled by
the programmer. If not, then a compilation error displays on the system.
Examples: Checked exceptions include SQLException and ClassNotFoundException.
2. Unchecked exceptions: Also called runtime exceptions, these exceptions occur during
program execution. These exceptions are not checked at compile time, so the programmer
is responsible for handling these exceptions. Unchecked exceptions do not give
compilation errors
Handling:
Handling is a method to handle/remove the exception.
Exception handling:
Exception handling is the process of responding to unwanted or unexpected events when a
computer program runs. Exception handling deals with these events to avoid the program or system
crashing, and without this process, exceptions would disrupt the normal operation of a program.
2
How exception is handled?
When an exception occurs, specialized programming language constructs, interrupt hardware
mechanisms or operating system interprocess communication facilities handle the exception.
Check the Inputs: Before our method starts doing its job, we check if the inputs (the things
we're given) are what we expect. If not, we throw an exception. It's like saying, "Wait a minute,
this is not what I asked for."
3
Handle the Exception: When we throw an exception, we're saying, "Hey, something's
wrong!" But we don't just stop there. We also say what should happen next. For example, if a
student shouts their favorite color, you might say, "That's not an answer. Please try again." In
programming, we can do something similar, like telling the program to try a different approach or
give an error message that makes sense
Why Check Inputs?
When a method receives information or data from another part of the program, we call this
information "input." Checking inputs is like making sure you're getting the right ingredients to
cook a meal. If you get the wrong ingredients, your meal might not taste good, and if you use them
without checking, it could even make someone sick! Similarly, in programming, if a method gets
the wrong input or doesn't check it, it might not work correctly, and it could cause problems in
your program.
Robust:
Robustness, in a general sense, refers to the ability of a system, process, or entity to
maintain its stability, functionality, or performance in the face of various challenges, changes, or
unexpected conditions. It is a quality or property that allows something to endure and adapt
effectively under adverse or uncertain circumstances.
Here's a brief overview of how exception handling works and how it can help make your code
more robust by checking inputs from calling objects:
Throwing Exceptions:
In many programming languages, including Java, C++, Python, and others, you
can use the throw keyword or a similar mechanism to raise an exception when an exceptional
condition is encountered in your code. This typically involves creating an object that represents
the exception (e.g., an instance of an exception class) and throwing it.
Syntax:
void divide(int numerator, int denominator) {
if (denominator == 0) {
throw std::runtime_error("Division by zero");
}
// Perform the division
}
Catching Exceptions:
To handle exceptions, you use a try...catch block or a similar construct in your
code. The try block contains the code that might throw an exception, while the catch block
4
specifies what should happen if an exception of a particular type is thrown. Multiple catch blocks
can be used to handle different types of exceptions.
Syntax:
try {
// Code that may throw exceptions
divide(10, 0);
} catch (const std::runtime_error& e) {
// Handle the exception
std::cerr << "Caught an exception: " << e.what() << std::endl;
}
Syntax:
try {
Benefits:
Checking inputs and making methods robust has several benefits:
1. Prevents Errors: It prevents your program from doing unexpected things or crashing
when it gets bad data.
2. Enhances Reliability: Your program becomes more reliable because it handles
invalid input gracefully instead of breaking.
5
3. Improves Security: It can also improve security by preventing malicious input from
causing harm.
6
}
} catch (int x) {
std::cout << "a is an integer: " << x << std::endl;
} catch (char x) {
std::cout << "a is a character: " << x << std::endl;
} catch (float x) {
std::cout << "a is a float: " << x << std::endl;
}
return 0;
}
Conclusion:
In programming, making methods robust by checking their inputs is like being a careful chef who
checks the ingredients before cooking. It ensures your program works well, avoids problems, and
keeps everything running smoothly. So, remember to check your inputs to create better and safer
software!