I am facing difficulty in using try/catch.
I am using try/catch whenever there is an error and its shows error message “ Unhandled exception type”.
If I click its showing suggestion “Surround with try/catch” , it automatically adds the try block and catch block. The problem is that after choosing like that so many times, my program becomes having many partitions with many try blocks and catch blocks. And then I even don’t know what information should be given in which catch block. What should I do for that problem?
What is the right and proper way to use try/catch?
And what should I do if the program should not proceed.
For example, in the following program, if the user choose “cmdWrite”, firstly, the system will try to open the recordstore named “Namelist”. If the record store cannot be open, there will be an exception. So I use try/catch block. But if the recordstore can not be open, it should not proceed the code following such as for loop here. How can I prevent that?
In this program, at the last part, I try to close the recordstore. But the recordstore may not be open because 0f some problem to open the recordstore.. So firstly I test whether the recordstore is open or not by writing if(rs!=null). Is it the correcy way?
To destroy the instance of RecordEnumeration, can we also test like that? if(enumeration!=null)enumeration.destroy();
I have written many recordstore programs regarding
Writing and Reading String Data Types,
Writing and Reading Mixed Data Types
Sorting Single Data Type Records in a RecordEnumeration
Sorting Mixed Data Type Records in a RecordEnumeration
Searching Single Data Type Records
Searching Mixed Data Type Records with various version of try/catch block. Some give me error when I close the MIDlet because of the position of closeRestore() method.
Code:package tcomparatorstring; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Item; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; public class Mainform extends Form implements CommandListener { Command cmdWrite; Command cmdRead; RecordStore rs; RecordEnumeration enumeration; Alert alert; public Mainform(String arg0) { super(arg0); cmdWrite=new Command("Write",Command.SCREEN,0); cmdRead=new Command("Read",Command.SCREEN,1); addCommand(cmdWrite); addCommand(cmdRead); setCommandListener(this); } public Mainform(String title, Item[] items) { super(title, items); // TODO Auto-generated constructor stub } public void commandAction(Command c, Displayable d) { if(c==cmdWrite) { String[] name={"No No","Ko Ko","Mo Mo"}; try { rs=RecordStore.openRecordStore("Namelist",true); } catch (RecordStoreException e) { // TODO Auto-generated catch block alert=new Alert("opening recordstore error",e.toString(),null,AlertType.ERROR); TComparatorStringMIDlet.tcomparatorstringmidlet.display.setCurrent(alert); } for(int i=0;i<3;i++) { try { rs.addRecord(name[i].getBytes(), 0, name[i].getBytes().length); } catch (RecordStoreException e) { alert=new Alert("writing recordstore error",e.toString(),null,AlertType.ERROR); TComparatorStringMIDlet.tcomparatorstringmidlet.display.setCurrent(alert); } } try { rs.closeRecordStore(); } catch (RecordStoreException e) { alert=new Alert("closing recordstore error",e.toString(),null,AlertType.ERROR); TComparatorStringMIDlet.tcomparatorstringmidlet.display.setCurrent(alert); } }//if(c==cmdWrite) if(c==cmdRead) { try { StringBuffer buffer=new StringBuffer(); rs=RecordStore.openRecordStore("Namelist", false); Comparator comparator=new Comparator(); enumeration=rs.enumerateRecords(null, comparator, false); while(enumeration.hasNextElement()) { buffer.append(new String(enumeration.nextRecord())); buffer.append('\n'); } alert=new Alert("Name List",buffer.toString(),null,AlertType.INFO); TComparatorStringMIDlet.tcomparatorstringmidlet.display.setCurrent(alert); //rs.closeRecordStore(); } catch (RecordStoreException e) { alert=new Alert("opening recordstore error",e.toString(),null,AlertType.ERROR); TComparatorStringMIDlet.tcomparatorstringmidlet.display.setCurrent(alert); alert.setTimeout(Alert.FOREVER); } if(enumeration!=null)enumeration.destroy(); if(rs!=null) try { rs.closeRecordStore(); //enumeration.destroy(); } catch (RecordStoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } }//if(c==cmdRead) } }

Reply With Quote


