This can used to make your application robust. You may throw exceptions where some illegal operation occurs within the program flow. Now we make a class along with main method
Listing 1: Defining class along with main method
public class ExceptionHandle
{
public static void main(String args[])
{
try
{
new ExceptionHandle().start();
}
catch(Exception e)
{
System.out.println("Small Informative"+e.getMessage()+"\n");
System.out.println("Detailed explaination");
e.printStackTrace();
}
finally
{
System.out.println("Always executed");
}
}
Here,
Now we will define the start method
Listing 2: Defining start method
public void start() throws Exception
{
int a=5,b=5,c=0;
try
{
int res=a/0;
}
catch(Exception e)
{
System.out.println("Small Informative"+e.getMessage()+"\n");
System.out.println("Detailed explaination");
e.printStackTrace();
}
c=a+b;
if(c!=10)
{
throw new Exception();
}
}
}
Here,
Listing 3: Full Source Code
public class ExceptionHandle
{
public static void main(String args[])
{
try
{
new ExceptionHandle().start();
}
catch(Exception e)
{
System.out.println("Small Informative"+e.getMessage()+"\n");
System.out.println("Detailed explaination");
e.printStackTrace();
}
finally
{
System.out.println("Always executed");
}
}
public void start() throws Exception
{
int a=5,b=5,c=0;
try
{
int res=a/0;
}
catch(Exception e)
{
System.out.println("Small Informative"+e.getMessage()+"\n");
System.out.println("Detailed explaination");
e.printStackTrace();
}
c=a+b;
if(c!=10)
{
throw new Exception();
}
}
}
Listing 4: Output
public class ExceptionHandle
{
public static void main(String args[])
{
try
{
new ExceptionHandle().start();
}
catch(Exception e)
{
System.out.println("Small Informative"+e.getMessage()+"\n");
System.out.println("Detailed explaination");
e.printStackTrace();
}
finally
{
System.out.println("Always executed");
}
}
This is all. Hope you liked the article. See you next time.







See the prices for this post in Mr.Bool Credits System below: