6 Replies - 341 Views - Last Post: 02 March 2013 - 11:17 AM Rate Topic: -----

#1 Hishermen  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 01-March 13

Throwing Exceptions Back to Main Method

Posted 02 March 2013 - 12:11 AM

Hello everyone I have a question I cant seem to find an answer for. I am reading a file and my assignment requires I handle a few different exceptions. Now it says that error messages must only print from the main method, so the exceptions must be thrown back to the main method to be printed. The parts I cannot figure out is how to determine where in the file the error occurs and how to throw the information back to the main. Here is the main method which asks the user to enter the name of files and catches errors if they occur.

public static void main(String[] args) throws IOException
	{
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the name for the univerals list of items");
		ItemList listOne = new ItemList();
		try
		{
			listOne.fillItemList(in.next());
		}
		catch (FileNotFoundException fnfe)
		{
			System.out.println("The file you entered does not exist. Enter a new name");
			listOne.fillItemList(in.next());
		}
		catch (ArrayIndexOutOfBoundsException aioobe)
		{
			System.out.println("The universal list file was not formatted correctly\nThere seems to be a problem with line(need to find out how to determine line)");
			System.out.println("This error was caused by " + aioobe.getClass());
		}


Here is a method in a class that takes a fileName and reads in each line and adds the information to an ArrayList<>

public void fillItemList(String fileName) throws IOException
	{
		File fileOne = new File(fileName);
		Scanner fileReader = new Scanner(fileOne);
		while (fileReader.hasNextLine())
		{
			String[] line = fileReader.nextLine().split(" ");// for items with three names
			if (line.length == 4)
			{
				String name = line[0] + " " + line[1] + " " + line[2];
				String upc = line[3];
				listOfItems.add(new Item (name, upc));
			}			
			if (line.length == 3)// for items with two names
			{
				String name = line[0] + " " + line[1];
				String upc = line[2];
				listOfItems.add(new Item (name, upc));
			}
			else //(line.length == 2)
			{
				listOfItems.add(new Item(line[0], line[1]));
			}
			
		}
	}


I am unsure how to get the information from an exception from the non main class to the main class. Do I need try/catch blocks in the non main class and throw the info back to the main or are they required in both. Also when reading the file I am required to report at what line in the file the error occurs. I was guessing having an int and adding one every time the while loop ran and reporting that if an exception occurs, but again i'm not sure how to report this information back to the main method.

Thanks in advance
Alex

Is This A Good Question/Topic? 0
  • +

Replies To: Throwing Exceptions Back to Main Method

#2 Limecore  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 91
  • Joined: 28-November 12

Re: Throwing Exceptions Back to Main Method

Posted 02 March 2013 - 01:12 AM

I think what you have to do is in your methods, don't catch errors, and possibly throw them if needed, and surround the call to the method in try catch blocks. If you use eclipse (or another ide), it should insert the throws part into your function declaration automatically, if not, you need to add to the end of the function declaration throws [errors]

public static void testMethod()Throws EOFException

Was This Post Helpful? 1
  • +
  • -

#3 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • Posts: 2,620
  • Joined: 29-July 11

Re: Throwing Exceptions Back to Main Method

Posted 02 March 2013 - 04:30 AM

Yeah, just throw them and handle them in main with try/catch.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: Throwing Exceptions Back to Main Method

Posted 02 March 2013 - 04:31 AM

Read up on exceptions. They are stack-oriented, in that they exist in a caller/callee relationship to each other.

The pattern you're seeking is this kind of thing. Never have anything but a mimimum amount of code in main:
    public static void main(String[] args) {
        try {
            new Main().runApp();

            // (All other methods declare that they throw exceptions [where necessary])
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }


This post has been edited by g00se: 02 March 2013 - 06:56 AM
Reason for edit:: generalisation

Was This Post Helpful? 0
  • +
  • -

#5 Hishermen  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 01-March 13

Re: Throwing Exceptions Back to Main Method

Posted 02 March 2013 - 09:00 AM

Yes your right about the methods throwing Exceptions and the class catching them. Currently my program handles that fine it throws in the non main methods and catches the exceptions in the main with no problem. The problem is the information that say getMessage() provides is not all that I need. I was wanted to know how to throw information that includes where in the file the error occured. For example a file looks like this

Bread 1011
Tea 1532
Milk BLAaasdpkfhpasdhf
Eggs 1012

When my program runs i'll get a NumberFormatException or something like that. Now while my program reports that and continues to run fine, I need to know what line the error occurred at and print it to the console from the main. In the above info the error would be line three. How would I report that information back to the main?
Was This Post Helpful? 0
  • +
  • -

#6 Hishermen  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 01-March 13

Re: Throwing Exceptions Back to Main Method

Posted 02 March 2013 - 10:24 AM

I have solved the problem. I needed a try block in my non main and in the catch block throw a custom exception with the original exception plus the line number and then catch it in the main method. Here was the code if anyone in the future needs it.

public void fillItemList(String fileName) throws IOException, CustomException
	{
		int lineNum = 0;
		File fileOne = new File(fileName);
		Scanner fileReader = new Scanner(fileOne);
		try
		{
			while (fileReader.hasNextLine())
			{
				lineNum++;
				String[] line = fileReader.nextLine().split(" ");// for items with three names
				if (line.length == 4)
				{
					String name = line[0] + " " + line[1] + " " + line[2];
					String upc = line[3];
					listOfItems.add(new Item (name, upc));
				}			
				if (line.length == 3)// for items with two names
				{
					String name = line[0] + " " + line[1];
					String upc = line[2];
					listOfItems.add(new Item (name, upc));
				}
				else
				{
					listOfItems.add(new Item(line[0], line[1]));
				}			
			}
		}
		catch (IndexOutOfBoundsException ioobe)
		{
			throw new CustomException(ioobe, lineNum);
		}
	}



public static void main(String[] args) throws IOException, CustomException
	{
		Scanner in = new Scanner(System.in);
		System.out.println("Enter the name for the univerals list of items");
		ItemList listOne = new ItemList();
		try
		{
			listOne.fillItemList(in.next());
		}
		catch (FileNotFoundException fnfe)
		{
			System.out.println("The file you entered does not exist. Enter a new name");
			listOne.fillItemList(in.next());
		}
		catch (CustomException ce)
		{
			System.out.println("The universal list file was not formatted correctly\nThere seems to be a problem with line " + ce.getLine());
			System.out.println("This error was caused by " + ce.getCause().getClass());
			
		}


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: Throwing Exceptions Back to Main Method

Posted 02 March 2013 - 11:17 AM

Yes, or use the pattern i posted and

throw new RuntimeException(String.format("Parse error occurred at line %d", lineNum),ioobe);

This post has been edited by g00se: 02 March 2013 - 11:18 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1