Download Errors in code Handling exceptions in Java and more Schemes and Mind Maps Software Development in PDF only on Docsity!
Errors in code
Handling exceptions in Java
Programming
- What is a good program? A program that is reliable.
- Not just giving correct answer on correct input
- Should protect against possible errors (invalid password, not a picture file, etc) Throwing Exceptions
How do we do this?
- Option 1 – return a special value to determine if the method succeeded or failed - Ex. Make withdraw return true or false
- What’s wrong with this?
- Calling method may not check answer
- Calling method may not know what to do
Catching Exceptions
- Exception – an error condition that can occur during the normal course of a program execution
- In Java, exceptions are objects themselves
- Exception handling is another form of control structure (like ifs and switch statements) - When an error is encountered, the normal flow of the
program is stopped and the exception is handled
Exception Program Flow
- What happens when exceptions occur?
- An Exception object is thrown
- What happens when an Exception is thrown?
- normal execution stops and exception handling begins
- What does the Exception object know?
- the name of the problem
- the location where it occurred
- and more…
Why have exception handling?
- consistency (everyone else does it)
- Java API classes use exceptions.
- Other programming languages do too!
- flexibility
- Programmer can decide how to fix problems.
- simplicity
- Easy to pinpoint problems.
public double divide( int data1, int data2 )
return data1 / data2;
What exceptions can occur?
Arithmetic Exception:
divide by zero
How to handle Exceptions? Do nothing
- program crashes if the exception occurs! Propagate (throws) it
- tell the method’s caller about it and let them decide what to do Resolve (try-catch) it in our method
- fix it or tell the user about it and let them decide what to do
- Says in English:
- System has caught an error described as a
InputMismatchException
- Thrown because a String cannot be converted to an integer
- When system handles, we often get a program crash
- Instead of the system, we can handle to improve robustness
Throwing Exceptions
- If there is an error in the value of a parameter, we can throw exceptions to make the user accountable
- Decide what type of exception to throw
- Test for condition, and throw the exception if condition is violated
Problem public class BankAccount { public void withdraw(double amount) { if (amount > balance) { ????????? } balance = balance - amount; }
... }
Solution #
public class BankAccount
public void withdraw(double amount)
if (amount > balance)
IllegalArgumentException exception
= new IllegalArgumentException("Amount
exceeds balance");
throw exception;
balance = balance - amount;
Checked/Unchecked Exceptions
- Checked Exception – checked at compile time - Complier ensures that you are handling a possible problem - Due to external circumstances that the programmer cannot prevent - Majority occur when dealing with input and output - For example, IOException
- Unchecked Exception – Runtime Exceptions
- Extend the class RuntimeException or Error
- They are the programmer's fault
- Examples of runtime exceptions:
- NumberFormatException
- IllegalArgumentException
- NullPointerException
- Optional to deal with these
- Example of error: OutOfMemoryError
- Can’t do anything about these catastrophic problems, so don’t deal with it