what is exception?
what is exception handling?
When occur Exception in a program?
Why we use exception handling macanisum?
explain try, catch, finally keywords.
try,catch and finally-Rules.
Types of Exception.
example of Some java Pre-defined /Built-in exception class.
User-defined Eceptions
Exception in java?
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Exception handling in java
1. Topics : Exception handling in java
created by……
Md .shohel Rana
Roll:140135
2nd year 2nd semester
Dept. of Cse
2. Exception Handling
An exception is an abnormal condition in which the
normal execution of code gets hampered at run
time.
The error handling mechanism that is called
exception handling.
3. When occur Exception in a program
Some of the common runtimes errors
1) Dividing a number by zero
2) Accessing an element that is out of bounds of an array
3) Using negative value as array size
4) Converting string to integer
5) File error
6) Corrupting memory
4. Why we use exception handling macanisum
Notify the error
Save all work
Allow graceful termination of a program
5. try, catch, finally keywords
the code which might throw some exceptions should be
kept in try block.
the catch block can hava the code to handle the
exception
finally block can be used to clean up code or release
some resources that are utilized in the program
6. using try,catch and finally keywords
There are three forms of try statement:
1) try-catch
A try block followed by one or more catch blocks.
2) try-finally
A try block followed by a finally blocks.
3) try-catch-finally
A try blocks followed by one or more catch blocks followed by a finally block.
7. using try,catch and finally keywords
Syntax:
Try{
//code that might cause an exception is kept here
}catch(ExceptionType obj){
//when an exception occurs, the control comes here
} catch(ExceptionType obj){
//when an exception occurs, the control comes here
}
8. using try,catch and finally keywords
Syntax:
Try{
//code that might cause an exception is kept here
}finally{
//release some resources here
}
9. using try,catch and finally keywords
Syntax:
Try{
//code that might cause an exception is kept here
}catch(ExceptionType obj){
//when an exception occurs, the control comes here
}finally{
//release some resources here
}
10. try,catch and finally-Rules
When there is a try block, catch or finally block should be
there
There should not be any statements between try and catch
and finally
Finally block is optional.
Only one finally block for a try block.
There can be multiple catch blocks for a try block.
12. Some java Pre-defined /Built-in exception class
Java.lang.Exception is a super class for all pre-defined exception class.
1. Exception ---------------------------------------------------> Base class for all exception objects.
2. SystemException------------------------------------------> Base class for all runtime errors.
3. ArithmeticException-------------------------------------> Arithmetic error.
4. ArrayIndexOutOfBoundsException-----------------> array index out of bounds
5. NegativeArraySizeException--------------------------> Array created with a negative size.
6. NullPointerException------------------------------------->invalid use of a null reference.
7. FileNotFoundException---------------------------------> when file is not found
8. ClassNotFoundException----------------------------- >when class is not found
9. NumberFormatException------------------------------> invalid conversion of a string ot a number format
14. IndexOutOfBoundsException
-------there are two types………..
1. ArrayIndexOutOfBoundsException
2. StringIndexOutOfBoundsException
……………………………………………..
int [] marks={3,4,5};
System.out.println(marks[4]); //Wrong index number
………………………………………………
String str="hello";
System.out.println(str.charAt(5)); //Wrong character index number
16. User-defined Exceptions
Most of the times when we are developing an application in java, we
often feel a need to create and throw our own exceptions.These
exceptions are known as user-defined or custom exceptions.
Point ot remember:
1. user-defined exception needs to inherit (extends) Exception class in
order to act as an exception.
2. throw keyword is used to throw such exceptions.
17. Using super() keyword
Part-1:
class DemoException extends Exception
{
public DemoException(String p)
{
super("i got an exception "+p); //call the main Exception class
}
}
18. Part-2:
public class userDef {
public static void main(String args[])
{
int balance = 100, withdraw = 1000;
if (balance < withdraw) {
try{
DemoException e = new DemoException("money is short");
throw e; //this throw the main exception class not DemoException class
}
catch(DemoException e) {
System.out.println("insufficient balancenn" + e);
}
}
else {
System.out.println("Draw & enjoy Sir, Best wishes of the day");
} }
}