Unit-5-exception-handling
Unit-5-exception-handling
Exception Handling
&
Generic Programming
What is an exception?
*An exception is an error condition
that changes the normal flow of
control in a program
2. Unchecked exceptions :
Known as Runtime Exceptions.
These include programming bugs,
such as logic error also.
Checked
exceptions
Example
import java.io.File;
import java.io.FileReader;
} Output:
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException
: 5 at
Exceptions.Unchecked_Demo.main(Unchec
ked_Demo.java:8
Exception Handling
Terms
1.Try – used to enclose a segment of code that may
produce a exception
try {
statements;
}
catch(Exception ex) {
perform operations before exits;
throw ex;
Multiple catch
statements
try {
<code segment that may
throw an exception..>
} catch (IOException e) {
System.out.println(e.getMessage());
} catch (FileNotFoundException e){
System.out.println(“FileNotFound!”);
}
Nested try-catch
block
try {
statement 1;
try {
statement 2;
statement 3;
}
catch(Exception e) { }
}
catch(Exception e) { }
By using Throw
THROW-generate an exception or to describe an
instance of an exception
Define a class:
public class EmptyStackException extends Exception {
}
Here is how you use the class:
public class Stack {
public Object Pop() throws EmptyStackException
{
if (Empty()) throw new EmptyStackException();
...
}
}
Note that you must use new to create an
exception object; you cannot just throw an
exception.
Example
static class Exception2{
static int sum(int num1, int num2){
if (num1 == 0)
throw new ArithmeticException("First parameter
is not valid");
else
System.out.println("Both parameters are
correct!!");
return num1+num2; }
public static void main(String args[]){
int res=sum(1,12);
System.out.println(res);
System.out.println("Continue Next statements");
}
}
The finally
try {
statements;
}
catch(TheExceptionex) {
handling ex;
}
finally {
finalStatements;
}
Example
public static void main(String[] arg){
try{
int i = 10/0;
} catch(Exception ex){
System.out.println("Inside 1st catch
Block");
} finally {
System.out.println("Inside 1st finally
block");
}
try{
int i = 10/10;
} catch(Exception ex){
System.out.println("Inside 2nd catch
Block");
} finally { Inside 1st catch Block
Inside
System.out.println("Inside 1stfinally
2nd finally block
block"); Inside 2nd finally block
Generics and Collections
Generic programming is a style of computer programming in which
algorithms are written in terms of types to-be-specified-later that are
then instantiated when needed for specific types provided as
parameters.
Compiling this fragment with J2SE 5.0 (or later) will yield a compile-time
error because the compiler will detect that list.get(2) returns String
instead of Integer
Entry<String, String> grade = new Entry<String,
String>("Mike", "A");
grade);
System.out.println("mark: " + mark);
if (prime.getValue())
System.out.println(prime.getKey() + " is prime.");
else
Output: System.out.println(prime.getKey() + " is not
prime.");
grade: (Mike,
A) mark: (Mike,
100)
13 is prime.
Note: If we remove the first <Type> in the above method, we will
get compilation error (cannot find symbol 'Type') since it
represents the declaration of the symbol. In many cases the user of
the method need not indicate the type parameters, as they can
be inferred:
Entry.<String>twic e("Hello");
Output:
java.lang.Integer = 11
java.lang.String =
GeeksForGeeks
java.lang.Double = 1.0
Programs that uses Generics has got many benefits over non-generic code.
1. Code Reuse: We can write a method/class/interface once and use for any type we
want.
2. Type Safety : Generics make errors to appear compile time than at run time (It’s
always better to know problems in your code at compile time rather than making
your code fail at run time).