Header

Wednesday 4 September 2013

IGNOU BCA 4th sem Solved Assignment - Explain the difference between checked and unchecked exceptions with example.

Explain the difference between checked and unchecked exceptions with example.
Ans
Unchecked exceptions :
  • represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time."
  • are subclasses of RuntimeException, and are usually implemented usingIllegalArgumentException, NullPointerException, or IllegalStateException
  • a method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)
  • represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
  • are subclasses of Exception
  • a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)

Unchecked exceptions can occur anywhere in a program and in a typical program can be very numerous. Therefore, the cost of checking these exceptions can be greater than the benefit of handling them. Thus, Java compilers do not require that you declare or catch unchecked exceptions in your program code. Unchecked exceptions may be handled as explained for checked exceptions in the following section.

All Exceptions that extend the RuntimeException class are unchecked exceptions.

Checked exceptions are exceptions that do not extend the RuntimeException class. Checked exceptions must be handled by the programmer to avoid a compile-time error. One example of a checked exception is the IOException that may occur when the readLine method is called on a BufferedReader object. Read more about the readLine method in the section on console input in the Java I/O page. All other exceptions you may have experienced were examples of unchecked exceptions.

There are two ways to handle checked exceptions. You may declare the exception using a throws clause or you may catch the exception. To declare an exception, you add the keyword throws followed by the class name of the exception to the method header. See example below. Any method that calls a method that throws a checked exception must also handle the checked exception in one of these two ways
Unchecked exceptions can occur anywhere in a program and in a typical program can be very numerous. Therefore, the cost of checking these exceptions can be greater than the benefit of handling them. Thus, Java compilers do not require that you declare or catch unchecked exceptions in your program code. Unchecked exceptions may be handled as explained for checked exceptions in the following section.
All Exceptions that extend the RuntimeException class are unchecked exceptions.

Checked exceptions are exceptions that do not extend th
e RuntimeException class. Checked exceptions must be handled by the programmer to avoid a compile-time error. One example of a checked exception is the IOException that may occur when the readLine method is called on a BufferedReader object. Read more about the readLine method in the section on console input in the Java I/O page. All other exceptions you may have experienced were examples of unchecked exceptions.

There are two ways to handle checked exceptions. You may declare the exception using a throws clause or you may catch the exception. To declare an exception, you add the keyword throws followed by the class name of the exception to the method header. See example below. Any method that calls a method that throws a checked exception must also handle the checked exception in one of these two ways
All Exceptions that extend the RuntimeException class are unchecked exceptions.
Checked exceptions are exceptions that do not extend the RuntimeException class. Checked exceptions must be handled by the programmer to avoid a compile-time error. One example of a checked exception is the IOException that may occur when the readLine method is called on a BufferedReader object. Read more about the readLine method in the section on console input in the Java I/O page. All other exceptions you may have experienced were examples of unchecked exceptions.

There are two ways to handle checked exceptions. You may declare the exception using a throws clause or you may catch the exception. To declare an exception, you add the keyword throws followed by the class name of the exception to the method header. See example below. Any method that calls a method that throws a checked exception must also handle the checked exception in one of these two ways
Checked exceptions are exceptions that do not extend the RuntimeException class. Checked exceptions must be handled by the programmer to avoid a compile-time error. One example of a checked exception is the IOException that may occur when the readLine method is called on a BufferedReader object. Read more about the readLine method in the section on console input in the Java I/O page. All other exceptions you may have experienced were examples of unchecked exceptions.
There are two ways to handle checked exceptions. You may declare the exception using a throws clause or you may catch the exception. To declare an exception, you add the keyword throws followed by the class name of the exception to the method header. See example below. Any method that calls a method that throws a checked exception must also handle the checked exception in one of these two ways
There are two ways to handle checked exceptions. You may declare the exception using a throws clause or you may catch the exception. To declare an exception, you add the keyword throws followed by the class name of the exception to the method header. See example below. Any method that calls a method that throws a checked exception must also handle the checked exception in one of these two ways
Checked exceptions :
It is somewhat confusing, but note as well that RuntimeException (unchecked) is itself a subclass of Exception (checked).
Unchecked exceptions are any class of exception that extends the RuntimeException class at some point in its inheritance hierarchy. This includes the most common exceptions. An ArithmeticException occurs when a program tries to divide by zero. A NullPointerException occurs when you try and access an instance data member or method of a reference variable that does not yet reference an object.


No comments:

Post a Comment