Chapter 10 :- Exception handling in Java

An exception is an indication of a problem that occurs during a program’s execution,it usually signals an error.Although exceptions occur in frequently,we must be careful to handle such cases while writing the code.
Errors can be broadly classified into two categories namely
Compile-time errors and
Run-time errors
Acompiler isused to convert source code into object code.Compile-time errors are usually the mistakes of a programmer and it won’t allow the program to compile unless they are solved.

Computer Science, “Exit code” or “Exit status” indicates whether the command or aprogram executed successfully or not.Code “0” indicates that the command executed successfully whereas code “1” indicates that some problem occurred while executing the command.

Thejava.lang andjava.io package contains ahierarchy of classes dealing with various exceptions.

An exception is an error condition. Exceptionhandlingis an object-oriented technique for managing errors. While performing exceptionhandlings we try to ensure that the program does not terminate abruptly nor does it generate unexpected output.

A try block contains the code that may give rise to one or more exceptions

A catch block contains the code that is intended to handle exceptions of a particular type that
were created in the associated try block.

A finally block is always executed before the programends,regardless ofwhether any exceptions are generated in the try block or not.
The try block :-
The try statement contains a block of statements within the braces. This is the code that we want to monitor for exceptions. Ifa problem occurs during its execution, an exception is thrown. Each type of problem (exception) corresponds to an object in Java.

Multiple Choice Question.

 

1. Which of the following refers to an error condition in object-oriented programming terminology?
a) anomaly
b) abbreviation
c) exception
d) deviation

2. Which of the following is a correct word for all Java Exceptions?
a) Runtime Exceptions
b) Throwables
c) Errors
d) Omissions

3. Which of the following statements is true?
a) Exceptions are more serious than Errors.
b) Errors are more serious than Exceptions.
c) Errors and Exceptions are equally serious.
d) Exceptions and Errors are the same thing.

4. Which of the following elements is not included in try block?
a) the keyword try
b) The keyword catch
c) the curly braces
d) statements that might cause Exceptions

5. Which of the following block handles or takes appropriate action when an Exception occurs ?
a) try
b) catch
c) throws
d) handles

6. Which of the following should be within a catch block?
a) finally block
b) Single statement that handles Exception
c) any number of statements to handle Exception
d) Throws keyword

7. What will happen when a try block does not generate an Exception and you have included multiple catch block?
a) they all execute
b) Only the first matching one executes
c) no catch block executes
d) Only the first catch block executes

8. Which of the following is an advantage of using a try ….. catch block?
a) Exceptional events are eliminated
b) Exceptional events are reduced
c) Exceptional events are integrated with the regular events
d) Exceptional events are isolated from regular events

9. Which of the following methods can throw an Exception?
a) methods with throws clause
b) methods with a catch block
c) Methods with a try block
d) methods with finally block

10. Which of the following is least important to know if you want to be able to use a method to its full potential?
a) the method’s return type
b) the type of arguments the method requires
c) The number of statements within the method
d) the type of Exceptions the method throws