954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to write code for try-catch statement to handle the exception?

how to write code include a try-catch statement to handle exception of this code. In the catch() statement, will provide a proper alert to the user on the error occurred.

import java.io.*;
import javax.swing.JOptionPane;
class TestArray
{
	public static void main (String []args)
	{
		double a;
		double w;
		double h;
		
		Mammal mamalia[]=new Mammal[2];
		Human people[] = new Human[3];
		
		for(int i=0;i<2;i++)
		{
			a =Double.parseDouble(JOptionPane.showInputDialog(null,"please enter the weight(kg) of mammalia "+(i+1)));
			mamalia[i]=new Mammal(a);
		}
		
		
		System.out.println("properties of mamalia 1 =");
		mamalia[0].display();
		System.out.println("species of mamalia 1  is  ="+mamalia[0].setSpecies("Homo sapiens"));
		System.out.println("properties of mamalia 2  =");
		mamalia[1].display();
		System.out.println("species of  mamalia 2 is = "+mamalia[1].setSpecies("Felis catus"));
		
		for(int i=0;i<3;i++)
		{
			h =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter height(m) of people  "+(i+1)));
			w =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter weight(kg) of people  "+(i+1)));	
			people[i] = new Human(h,w);
		}
		
		String b = JOptionPane.showInputDialog(null,"please enter name of people 1 ");
		people[1].setName(b);
		System.out.println("name of people 1 is "+people[1].setName(b));
		
		
		for(int i=0;i<3;i++)
		{
			people[i].display();
			System.out.println("BMI of people "+(i+1)+(" is ")+people[i].getBMI());
		}
		
	}
}
long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

you can catch array index out of bound exception.

rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
 
try {
  // your code that could cause an exception to be thrown
} catch (Exception e) { // catch any Exception that may be thrown
  // your code to display an error message
}
JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
you can catch array index out of bound exception.

Never use try-catch for ArrayIndexOutOfBoundsException. You already know at runtime the size or length of what you are trying to use. So always check that.

long89:
So when looping, it is good to do this instead of what you have:

for (int i=0;i<people.length;i++) {
 // use people[i]
}
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
try {
  // your code that could cause an exception to be thrown
} catch (Exception e) { // catch any Exception that may be thrown
  // your code to display an error message
}
import java.io.*;
import javax.swing.JOptionPane;
class TestArray
{
	public static void main (String []args)
	{
		double a;
		double w;
		double h;
		
		Mammal mamalia[]=new Mammal[2];
		Human people[] = new Human[3];
		
		try
		{
		
		for(int i=0;i<2;i++)
		{
			a =Double.parseDouble(JOptionPane.showInputDialog(null,"please enter the weight(kg) of mammalia "+(i+1)));
			mamalia[i]=new Mammal(a);
		}
		
		for(int i=0;i<3;i++)
		{
			h =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter height(m) of people  "+(i+1)));
			w =Double.parseDouble(JOptionPane.showInputDialog(null,"Enter weight(kg) of people  "+(i+1)));	
			people[i] = new Human(h,w);
		}
		
		}
		
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null,"wrong entering data types");
		}
		
		
		
		System.out.println("properties of mamalia 1 =");
		mamalia[0].display();
		System.out.println("species of mamalia 1  is  ="+mamalia[0].setSpecies("Homo sapiens"));
		System.out.println("properties of mamalia 2  =");
		mamalia[1].display();
		System.out.println("species of  mamalia 2 is = "+mamalia[1].setSpecies("Felis catus"));
		
		
		String b = JOptionPane.showInputDialog(null,"please enter name of people 1 ");
		people[1].setName(b);
		System.out.println("name of people 1 is "+people[1].setName(b));
		
		
		for(int i=0;i<3;i++)
		{
			people[i].display();
			System.out.println("BMI of people "+(i+1)+(" is ")+people[i].getBMI());
		}
		
	}
}


already solve:D

long89
Newbie Poster
11 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

please makes it as solve.

rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You