Java Exception Handling Notes
Java Exception Handling Notes
1. class TestMultipleCatchBlock1{
2. public static void main(String args[]){
3. try{
4. int a[]=new int[5];
5. a[5]=30/0;
6. }
7. catch(Exception e){System.out.println("common ta
sk completed");}
8. catch(ArithmeticException e)
{System.out.println("task1 is completed");}
9. catch(ArrayIndexOutOfBoundsException e)
{System.out.println("task 2 completed");}
10. System.out.println("rest of the code...");
11. }
12. }
The above code generate compile time error.
1. ....
2. try
3. {
4. statement 1;
5. statement 2;
6. try
7. {
8. statement 1;
9. statement 2;
10. }
11. catch(Exception e)
12. {
13. }
14. }
15. catch(Exception e)
16. {
17. }
Java nested try example
1. class Excep6{
2. public static void main(String args[]){
3. try{
4. try{
5. System.out.println("going to divide");
6. int b =39/0;
7. }
8. catch(ArithmeticException e)
9. {System.out.println(e);
10. }
11. try{
12. int a[]=new int[5];
13. a[5]=4;
14. }
15. catch(ArrayIndexOutOfBoundsException e)
16. {System.out.println(e);
17. }
18. System.out.println("other statement);
19. }
20. catch(Exception e)
21. {System.out.println("handeled");
22. }
23. System.out.println("normal flow..");
24. }
25. }
1. class TestFinallyBlock1{
2. public static void main(String args[]){
3. try{
4. int data=25/0;
5. System.out.println(data);
6. }
7. catch(NullPointerException e)
8. {System.out.println(e);
9. }
10. Finally
11. {System.out.println("finally block is always execu
ted");
12. }
13. System.out.println("rest of the code...");
14. }
15. }
Case 3
1. class TestExceptionPropagation2{
2. void m(){
3. throw new java.io.IOException("device error");//c
hecked exception
4. }
5. void n(){
6. m();
7. }
8. void p(){
9. try{
10. n();
11. }catch(Exception e)
{System.out.println("exception handeled");}
12. }
13. public static void main(String args[]){
14. TestExceptionPropagation2 obj=new TestExce
ptionPropagation2();
15. obj.p();
16. System.out.println("normal flow");
17. }
18. }
throws keyword
The Java throws keyword is used to declare an exception. It gives an information to the
programmer that there may occur an exception so it is better for the programmer to provide the
exception handling code so that normal flow can be maintained.
Exception Handling is mainly used to handle the checked exceptions. If there occurs any
unchecked exception such as NullPointerException, it is programmers fault that he is not
performing check up before the code being used.
Syntax of java throws
1. return_type method_name() throws exception_class_nam
e{
2. //method code
3. }
Which exception should be declared
public
InsufficientFundsException(double
amount) {
this.amount = amount;
}
try {
System.out.println("\nWithdrawing
$100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing
$600...");
c.withdraw(600.00);
} catch
(InsufficientFundsException e) {
System.out.println("Sorry, but
you are short $" + e.getAmount());
e.printStackTrace();
}
}
}
Output
Depositing $500...
Withdrawing $100...
Withdrawing $600...
Sorry, but you are short $200.0
InsufficientFundsException
at
CheckingAccount.withdraw(CheckingAccoun
t.java:25)
at
BankDemo.main(BankDemo.java:13)
throw throws
Java throw keyword is used to explicitly Java throws keyword is used to declare an
1)
throw an exception. exception.
Checked exception cannot be propagated Checked exception can be propagated with
2)
using throw only. throws.
3) Throw is followed by an instance. Throws is followed by class.
4) Throw is used within the method. Throws is used with the method signature.
You can declare multiple exceptions e.g.
5) You cannot throw multiple exceptions. public void method()throws
IOException,SQLException.