JavaSession 6
JavaSession 6
try :- try block is used in that part of code where Exception can be occured.
catch :- It catch the Exception and which Exception is occured and print the
messages.
Syntax:-
try{
//code where Exception can be occured
}
catch(type of Exception)
{
//print mssg }
----------------------------1-------------------
import java.util.*;
import javax.swing.text.html.HTMLEditorKit.Parser;
// String str=null;
// System.out.println(str.length());
}
}
-------------------------2-----------------
import java.util.*;
import javax.swing.text.html.HTMLEditorKit.Parser;
try {
System.out.println(var1/var2); }
catch(ArithmeticException e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("It Runs Always");
}
}
}
-----------------------------3----------------------------
import java.util.*;
import javax.swing.text.html.HTMLEditorKit.Parser;
try {
System.out.println(var1/var2);
System.out.println(ar[5]); }
catch(IndexOutOfBoundsException | ArithmeticException e)
{
System.out.println(e.getMessage());
}
finally
{
System.out.println("It Runs Always");
}
}
}
------------------------------4-------------------------
import java.io.IOException;
import java.util.*;
import javax.swing.text.html.HTMLEditorKit.Parser;
void calling()
{
try {
f2();
}
catch (Exception e) {
System.out.println("Handled");
}
}
}
}