In: Economics
Explain the application of the Perfect Tender Rule and its exceptions. Discuss the following:
Do these exceptions make sense to you? Why or why not?
If you are purchasing goods and want to do your best to be certain that an exception does not apply, how can you protect yourself?
Exception handling is the process of responding to the occurrence, during computation, of exceptions – anomalous or exceptional conditions requiring special processing – often changing the normal flow of program execution.
Handling the exception is nothing but converting system error
generated message into user friendly error message. Whenever an
exception occurs in the java application, JVM will create an object
of appropriate exception of sub class and generates system error
message, these system generated messages are not understandable by
user so need to convert it into user friendly error message. You
can convert system error message into user friendly error message
by using exception handling feature of java.
For Example: when you divide any number by zero then system
generate / by zero so this is not understandable by user so you can
convert this message into user friendly error message like Don't
enter zero for denominator.
Provide a couple of examples of exceptions and why you would use them.
Exceptions are a wonderful aspect of programming languages because they allow you to notice when something has gone wrong and deal with it gracefully. Without exceptions your application would end up presenting errors to the user and it would be much more difficult to diagnose what went wrong if your code failed silently.
Unfortunately exceptions are usually not one of the first things you learn when you start to code. In fact, you probably pick up a lot of bad habits when learning how to deal with problems because exceptions can seem like a difficult concept to get your head around.
Exceptions are actually a really simple thing to learn and they will make your code better and your life as a developer easier. Once you understand what an exception is and when and how you should use them you will find using exceptions in your code becomes second nature.
Please provide an example of at least one checked and one unchecked exception?
checked exceptions:
Checked exceptions are checked at compile-time. It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error.
Checked Exception Example
In this example we are reading the file myfile.txt and displaying its content on the screen. In this program there are three places where a checked exception is thrown as mentioned in the comments below. FileInputStream which is used for specifying the file path and name, throws FileNotFoundException. The read() method which reads the file content throws IOException and the close() method which closes the file input stream also throws IOException.
Unchecked exceptions:
Unchecked exceptions are not checked at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. Most of the times these exception occurs due to the bad data provided by user during the user-program interaction. It is up to the programmer to judge the conditions in advance, that can cause such exceptions and handle them appropriately. All Unchecked exceptions are direct sub classes of RuntimeException class.
Why might you need a finally clause?
The finally clause is used to provide the capability to execute code no matter whether ornot an exception is thrown or caught.