1

my question scenario is i have program in which arithmetic exception but i know how to catch exception.The thing is i need to store exception in some Data Structure,later i need to retrive,which Data structure is suitable. Here is my code.

public class ExceptionTest { public static void main(String[] args) { int i = 10, j = 0; try { System.out.println(i / j); } catch (Exception ex ) { //need to store exception in some DS(which one is best Suitable and why? System.out.println( ex.getMessage() ); } } } 

Please guide me.Your help will be appreciated.

11
  • 1
    That doesn't sound like a good idea. Can you give some hints on why you want to do this? There might be other ways of achieving it. Commented Jan 15, 2015 at 4:30
  • HashMap is sutiable for this case. Commented Jan 15, 2015 at 4:30
  • Stack, using this you can also get the order of exception. By the way why you want to store exceptions? what do you want to achieve? Commented Jan 15, 2015 at 4:31
  • @sandipon,why hash map is suitable,please give me some explonation.+ Commented Jan 15, 2015 at 4:32
  • @Sarz,i need to store exception ,again i should retrieve exception from data structure and print on console. Commented Jan 15, 2015 at 4:33

2 Answers 2

0

If you just want to have access to start or end elements (Exceptions) you can use a LinkedList mostly in form of stack or queue.

Next up can be HashSet which will prep for fast retrieval but out of order traversal.

Sign up to request clarification or add additional context in comments.

4 Comments

nash_ag,among all types of Data Structure ,LinkedList or HashSet is suitable for my above requirement?
LinkedList would help you preserve the order, if that is not important then go for HashSet.
nash_ag,before coming to conclusion could you please tell me your answer is better or (Mital Awachat) his answer is best.
The other answer gives you an option of ArrayList which can be helpful only in case of random reads (performance basis). Again iterating the fact that there is no one best data structure. It depends on your requirement.
0

You may maintain ArrayList to store your exceptions.

 ArrayList<Exception> exceptionList = new ArrayList<>(); exceptionList.add(exception); 

To persist exceptionList you may use ObjectOutputStream.

Full Code:

import java.util.ArrayList;

public class ExceptionTest {

public static void main(String[] args) { ArrayList<Exception> exceptionList = new ArrayList<>(); int i = 10, j = 0; try { System.out.println(i / j); } catch (Exception ex) { //need to store exception in some DS(which one is best Suitable and why? exceptionList.add(ex); System.out.println(ex.getMessage()); } } 

}

2 Comments

Mital Awachat,is it suitable for my above requirement and best among all data structure?
Yes, you may store, retrieve and read all exceptions. I hope following snippet helps: // RETRIEVING ALL EXCEPTIONS for (Exception exception : exceptionList) { System.out.println(exception.getMessage()); } // PESISTING try { ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream("file.txt")); outputStream.writeObject(exceptionList); } catch (Exception ex) { ex.printStackTrace(); }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.