30/12/2019

Exception Handling in Java :Studywow | Exception

Studywow: Detailed topic of Exception Handling in Java. Read more topics on Java (click here)

Q1. What is exception handling in Java?

Ans. Exception are used to handle errors or any other exceptional event that occurs in the normal flow of program. In java, exception handling is one of the mechanism to handle the runtime errors of the application programs.

Q2. Write the program to handle multiple exceptions in Java?

Ans. public class Main {
public static void main(String args[]) {
int array[] = {20,20, 40};
int num1=15, num2=0;
int result=0;
try {
result = num1/num2;
System.out.println("The result is" +result);
for (int i =2; i >=0; i--) {
System.out.println("The value of array is" +array[i]);
} }
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("Error ▢. Array is out of Bounds"+e);
}
catch (ArithmeticException e) {
System.out.println("Can't be divided by Zero"+e);
} }
}
Output: Can't be divided by Zero.

Q3. List out the advantages and disadvantages of default exception handler?

Ans. Exception Handling : It is the process of responding to the occurrence, during computation, of exceptions-anomalous or exceptional events requiring special processing-often changing the normal flow of program execution.

Advantages : The advantages of default exception handler are as follows:

1. Separating Error-Handling Code from "Regular" Code:

Exception provide the means to separate the details of what to do when something out of the ordinary happens from the main logic of a program. It enable you to write the main flow of your code and to deal with the exceptional cases elsewhere.

2. Grouping and Differentiating Error Types :

Because all exceptions thrown within a program are objects, the grouping or categorising of exceptions is a natural outcome of the class hierarchy.

3. Propagating Errors up the Call Stack:

It has the ability to propagate error reporting up the call stack of methods.

Disadvantages : The disadvantages of default exception handler are as follows:

1. Unchecked Exception :

Due to the use of unchecked exceptions which don't require the compilance of catch-or-specify requirement. This condition is abused by wrapping a checked exception inside a runtime exception and throwing it up the call stack.

2. Differentiation between Errors:

This mechanism does not distinguish between things that go wrong. As discussed below :
public static int readInt (PrintWriter pw, BufferedReader br String prompt)
throws Exception
if (prompt ! = null) {
pw.print (prompt);
pw.flush();
}
return Integer.parseInt (br.readLine());
//readInt (PrintWriter, BufferedReader, String)

Q4. How the exception handling occur in JDBC?

Ans. Exception handling allows you to handle exceptional conditions such as program-defined errors in a controlled fashion. When an exception condition occurs, an exception is thrown. The term thrown means that current program execution stops and control is redirected to the nearest applicable catch clause. If no applicable catch clause exists, then the program's execution ends.
JDBC exception handling is very similar to Java exception handling but for JDBC, the most common exception you'll deal with is Java.sql.SQLException.

SQLException Methods:

A SQLException can occur both in the driver and the database. When such an exception occurs, an object of type SQLException will be passed to the catch clause.
The passed SQLException object has the following methods available for retrieving additional information about the exception :

*Methods and their Descriptions :-

getErrorCode : Gets the error number associated with the exception.
getMessage() : Gets the JDBC driver's error message for an error handled by the driver or gets the oracle error number and message for a database error.
getSQLState : Gets the XOPEN SQLstate string. For a JDBC driver error, no useful information is returned from this method. For a database error, the five-digit XOPEN SQLstate code is returned. This method can return null.
getNextException() : Gets the next exception object in the exception chain.
printStackTrace() : Prints the current exception, or throwable and its backtrace to a standard error stream.
printStackTrace(PrintStream s) : Prints this throwable and its backtrace to the print stream you specify.
printStackTrace(PrintWriter w) : Prints this throwable and its backtrace to the print writer you specify.

Studywow: Read More about Java (click here)
Join us on Facebook and Twitter  to get the latest study material. You can also ask us any questions.
Facebook = @ studywow
(click on it or search "studywow" on facebook)
Twitter = @ studywow
(click on it or search "studywow" on Twitter)
Email= studywow.com@gmail.com
Send us your query anytime!


abcd