Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Exception handling using Java

This program can help you to learn, how you may handle exception in your programs ie making your application Robust.

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,

  1. We define a class named ExceptionHandle
  2. We define a main method and we call the start method of this class
  3. Now we make use of try-catch statement so that if any exception occurs while trying to execute the start method then this exception will be caught by the catch block. All the statements which may possibly cause an exception are kept inside the try block.
  4. Inside catch block we make use of getMessage method to get a message regarding the error which was caused. It gives a short description of the exception which occurred.
  5. Now to get a detailed explanation of the exception we can make use of printStackTrace method which will print how program executed and will show at what particular place the exception occurred. This can be very helpful in debugging the bug
  6. The final block is always called regardless of whether the exception occurs or not.

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,

  1. We define 3 variables a,b,c and initialize them
  2. Now we define a try block so that if any exception occur within this block then it can be captured and reported.
  3. We divide the value of a by 0 purposefully so that an exception is generated and program control now move to the catch part which will report the exception to the user.
  4. After that we sum a and b and put their value in c. Since a and b are 5 each so c must be 10
  5. But suppose if value of c is not 10 then we throw an exception by using throw as shown in the code.

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.



My main area of specialization is Java and J2EE. I have worked on many international projects like Recorders,Websites,Crawlers etc.Also i am an Oracle Certified java professional as well as DB2 certified

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

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

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login