Opening Hours :7AM to 9PM
Following predefined classes are will raise different system defined error messages as a java developer we must convert these messages into user-friendly messages with help of exception handling mechanism.
Throwable is a predefined class used to handle all type of exceptions and this is super class for both error and exception class.
Whenever we are providing wrong input values or writing wrong logics there may be a chance of raising exceptions and that can be track and handled shown below.
In java language exception handling can be achieved using try-catch block construction. Try and catch blocks are the keywords in the java language, it is used to convert System Defined Error Messages into User Friendly Error Messages very efficiently.
Try block always contains logic of the program or problematic code, catch block contains User Friendly Error Messages.
Syntax:
try
{
Logic of the code
}
catch(NameOfTheExcepton object)
{
User Friendly Message
}
catch(NameOfTheExcepton object)
{
User Friendly Message
}
..
..
import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
System.out.println("Enter x,y values");
Scanner s=new Scanner (System.in);
try
{
x=s.nextInt();
y=s.nextInt();
z=x/y;
System.out.println("Division Of Two Number Is "+z);
}
catch(ArithmeticException e)
{
System.out.println("Denominator should not be zero");
}
}
}
import java.util.*;
class Main
{
public static void main(String args[])
{
int x,y,z;
System.out.println("Enter x,y values");
Scanner s=new Scanner (System.in);
try
{
x=s.nextInt();
y=s.nextInt();
z=x/y;
System.out.println("Division Of Two Number Is "+z);
}
catch(ArithmeticException e)
{
System.out.println("Denominator should not be zero");
}
catch(InputMismatchException ee)
{
System.out.println("Please Enter Numbers Only");
}
}
}
try
{
..
..
}
catch(Exception objref)
{
System.out.error(objref);
}
class Main
{
public static void main(String args[])
{
int x,y,z;
try
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("z="+z);
}
catch(Exception e)
{
System.out.print(e);
}
}
}
class Main
{
public static void main(String args[])
{
int x,y,z;
try
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("z="+z);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
try
{
--
--
catch(ArithmeticException ae)
{
// these are individual exception handlers
--
--
}
catch(NumberFormatException ne)
{
--
--
}
catch(Exception e)
{
System.out.prinln(e); // this is common exception handler
}
class Main
{
public static void main(String args[])
{
int x,y,z;
try
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("z="+z);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
returntype methodname (lp/np) throws Exceptionclass1,...
{
--
--
}
try
{
obj.methodname();
--
--
}
catch(Exception obj)
{
--
--
}
class A
{
void f1() throws ArrayIndexOutOfBoundsException,StringIndexOutOfBoundsException
{
String v="hello";
char c=v.charAt(0);
int a[]=new int[5];
a[15]=20; \\ correct statement is a[1]=20;
System.out.println("a[15]="+a[15]); //correct statement is System.out.println("a[1]="+a[1]);
}
void f2() throws ArithmeticException
{
int x=10,y=0,z;// correct statement is int x=10,y=2,z;
z=x/y;
System.out.println("z="+z);
}
}
class B
{
void f4(String s1,String s2) throws Exception
{
int f1=Integer.parseInt(s1);
int f2=Integer.parseInt(s2);
int f3=f1+f2;
System.out.println("f3="+f3);
}
}
class Main
{
public static void main(String args[])
{
A a=new A();
B b=new B();
try
{
a.f1();
a.f2();
b.f4("ten","20"); \\correct statement is dm.f4("10","20");
}
catch(StringIndexOutOfBoundsException se)
{
System.out.println("String index is outof range");
}
catch(ArrayIndexOutOfBoundsException ee)
{
System.out.println("array index is outof range");
}
catch(ArithmeticException ae)
{
System.out.println("Denominator should not be zero");
}
catch(NumberFormatException ee)
{
System.out.println("please enter integer values only");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class Demo
{
public static void main(String args[])throws IOException
{
InputStream is=new InputStream(-----);
is.read(----); //raise to exception
}
}
finally
{
Set of excitable statements of try block
}
class Main
{
public static void main(String args[])
{
int x=10,y=0,z;
try
{
z=x/y;
System.out.println("Z="+z);
//System.out.println("hello");
}
catch(ArithmeticException ae)
{
System.out.println("Denominator should not be zero");
}
finally
{
System.out.println("hello");// try block statement
}
}
}
In the above example either exception is raised or not raised the statements of finally block executed .
Note:
the statements whatever is executing with in finally block these are generally try block statements.
If any exception is designed by the user and it if used commonly by specific number of developers is known as user defined exception.
If any exception is unable to handle by the JVM then those type of exceptions can be handled by the user by creating separate execution block.
Rules to design user defined exception:
1.Create a package with valid user defined name.
2.Create a public class under given package with valid user defined name.
3.Extends either runtime exception or exception class to the user defined class.
4.Create a public constructor with string parameter.
5.Call the super class constructor with a derived class constructor.
6.Save the program with exception class name.java
Syntax:
package PackageName
public class ClassName extends Exception
{
publicclassname(String variablename)
{
super(String variable);
}
}
In the above example (Syntax) String variable can be used to common exception message and main aim of creation of user defined exception class is to package the exception message commonly to all it’s super class.