Exception Handling

Exception Handling in Java
Exception refers to any contradictory or unusual situation which can be encountered during a program execution. In Java Exceptions (or Errors) has been categorized broadly under three heading –
1. Syntax Errors – A Syntax error occurs when the programmer writes a grammatically incorrect code that does not support the rules of programming of that particular language. This type of error gets detected by the compiler. E.g. missing semicolon, using keywords as identifier name, unmatched pair of { }, undefined functions, undeclared and un-initialized variables etc…
2. Logical Errors – A Logical error is born out of a programmer who misunderstands the logic of the program and writes a syntactically correct code but which does not comply to the algorithm of the problem. E.g. The program demanded squaring of an integer type number but the programmer writes a code that determines the square root of the number.
3. Runtime Errors – A Runtime error is shown when a syntactically and logically correct program with a set of inputs which is invalid for a given expression. The same expression is valid for another set of values. E.g. int quo = div / n, the expression is valid for all sets of positive values of n but not if n = 0.
Exception handling is ideal for –
1. Processing exceptional situations.
2. Processing exceptions for components which cannot handle them directly.
3. Processing exceptions for widely used components that should not process their own exceptions.
4. Large Projects that requires uniform error processing.
Advantages of exception handling:-
1. It separates error handling code from the normal code.
2. It enhances the readability.
3. It makes clear and strong program code.
The process of exception handling is implemented using a try … catch block, in which the try block contains the code which has probability of having errors during runtime and the catch block contains code to handle the situation.
Common Errors
0 < x < 100
Comparison operators can be used with two numbers. Although you can write 0 < x = or <= instead of ==. For example, because the decimal number 0.1 can not be represented exactly in binary, (0.1 + 0.1 + 0.1) is not equal to 0.3
String issues using = = and equals ( )
Usually you want to know if the two Strings have the same value, for e.g.
if (name == “Californication”) // Legal, but SURELY WRONG
Reason is the = = operator is not comparing the values of name and Californication instead it is just comparing the references i.e. it is checking whether both of them are strings or not and hence returns true and to the programmer it reveals to be perfectly LEGAL. To prove this we can take another e.g. say, –
String name = “Californication”;
System.out.println (name. substring (0,3)); // Gives the output as Cal
But when they are compared using = = gives the output as Un-Equal
if (name. substring (0,3) = = “Cal”)
System.out.println (“Equal”);
else
System.out.println (“Un-Equal”);
Hence always use equals ( ) method of String class to compare the values of String class objects.
NaN
No exceptions are generated by floating-point operations. Instead of an interruption in execution, the result of an operation may be positive infinity, negative infinity, or NaN (not a number). Division by zero or overflow produces infinity. Subtracting two infinities produces a NaN.
Infinity
Similarly no exceptions are generated for statements like Math.pow(0,–1) and such statements will result in Infinity as output

Comments