/* related posts with thumb nails */

Exception Handling:

1. What are errors?

Errors are the wrongs that can make a program go wrong. In computer terminology errors may be referred to as bugs.

2. What are the types of errors?

Errors may broadly be classified into three categories:

Compile-time errors

Run-time errors

Logical errors

3. Give examples for compile-time errors

Missing semicolons

Missing (or mismatch of) brackets in classes and methods

Misspelling of identifiers and keywords

Missing double quotes in strings

Bad references to objects

Use of = in place of = = operator

4. Give examples for run-time errors

Dividing an integer by zero

Accessing an element that is out of the bounds of an array

Passing a parameter that is not in a valid range or value for a method

Trying to illegally change the state of a thread

Attempting to use a negative size for an array

Converting invalid string to a number

5. What is Exception and Exception Handling?

Handling runtime errors is exception handling. We use try-catch block mechanism for exception handling.

6. What is an Exception?

Exception is a condition caused by a runtime error in a program. It is an abnormal event that arises during the execution of the program and terminates the normal flow of the program.

7. What are advantages of Exception-handling?

· It ensures safer termination of a program.

· It demands proper input entry or proper handling of a program.

· User has a chance to correct the accidental mistakes while running a program.

· The program becomes robust and more user-friendly.

· With this mechanism the working code and the error-handling code can be disintegrated.

· It allows different handling code-blocks for different types of errors.

8. What is try-block?

“try” block contains the code in which runtime error may occur. It throws Exception object. A try block must be followed by at least one catch block or a finally block. But, it can be followed by multiple catch blocks.

9. What is catch-block?

“catch” block contains the handling code for runtime errors i.e. when run time error occurs, instead of terminating the program, control comes to “catch” block. Program control enters into “catch” block only when the corresponding runtime error occurs.

10. What is finally-block?

The finally block is always executed, regardless of whether or not an exception is thrown. It is recommended for actions like closing file streams and releasing system resources.

11. What are system defined exception classes?

All exceptions are system defined classes. They are subclasses of either ‘Exception’ or ‘RuntimeException’ class. We can also instantiate them. Some common system-defined exceptions are listed below

Exception Type

Cause of Exception

ArithmeticException

Caused by math errors such as division by zero

ArraylndexOutOfBoundsException

Caused by bad array indexes

ArrayStoreException

Caused when a program tries to store the wrong type of data in an array

FileNotFoundException

Caused by an attempt to access a nonexistent file

lOException

Caused by general I/O failures, such as inability to read from a file

NullPointerException

Caused by referencing a null object

NumberFormatException

Caused when a conversion between strings and number fails

OutOfMemory Exception

Caused when there's not enough memory to allocate a new object

SecurityException

Caused when an applet tries to perform an action not allowed by the browser's security setting

StackOverFlowExceptlon

Caused when the system runs out of stack space

StringlndexOutOfBoundsExceptlon

Caused when a program attempts to access a nonexistent character position in a string

12. How do you create a user-defined exception>

A user defined exception can be created be extending Exception class i.e. that class must be the sub-class of Exception.

13. What are checked exceptions and unchecked exceptions?

There are two types of Exceptions in Java:

Checked Exceptions

Unchecked Exceptions

Checked Exceptions: These are the exceptions, which occur during the compile-time of the program.

InstantiationException

IllegalAccessException

ClassNotFoundException

NoSuchMethodException

InterruptedException

Unchecked Exceptions: Unchecked exceptions are the exceptions which occur during the runtime of the program. Unchecked exceptions extend the RuntimeException.

IndexOutOfBoundsException

ArrayIndexOutOfBoundsException

ClassCastException

ArithmeticException

NullPointerException

14. What is throw?

‘throw’ is a keyword used in exception handling mechanism. It is used to raise the errors manually.

15. What is throws clause?

If a method is capable of causing an exception that it does not handle, it must specify this behavior (that it has chance of raising exception) so that callers of the method can guard that block against that exception.

Related Topics:

0 comments:

Post a Comment