×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    how to use try/catch?

    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)
    		
    		
    	}
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: how to use try/catch?

    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”.
    this means you are trying to capture some specific exception and missing the actual, just try to Capture Exception e

    like

    try{
    // what you want to try
    }catch(Exception e)
    {
    System.out.println("Exception "+e);
    }

    You will see the correct exception in SOP.
    You can put it multiple catch() blocks and in last put this catch(Exception e), to avoid such issue as you are facing now,.
    Thanks,
    Ekta

  3. #3
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    Re: how to use try/catch?

    I am sorry. I do not get what you said.
    Do u mean that I can put all my coding only in one try block and finally use only one catch block? totally one try block and one catch block?

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Noida
    Posts
    941

    Re: how to use try/catch?

    Quote Originally Posted by komomo View Post
    I am sorry. I do not get what you said.
    Do u mean that I can put all my coding only in one try block and finally use only one catch block? totally one try block and one catch block?
    No, I don't you to put in all code in 1 try/catch black.....

    Use try/catch for any code line where you suspect any exception in runtime.

    For a Single Try, you can put multiple catch().... try to catch the exception you feel you might get on that codelet which in try block and later add an extra catch block to capture Exception also

    Like this:

    try {
    rs=RecordStore.openRecordStore("Namelist",true);
    } catch (RecordStoreException e) {
    // TODO Auto-generated catch block
    }catch(IOException ioe) // to catch any other exception if not captured in above catch blocks.
    {
    System.out.println("Exception "+ioe);
    }catch(Exception e) // to catch any other exception if not captured in above catch blocks.
    {
    System.out.println("Exception "+e);
    }

    As Exception is supper class of all other types of Exceptions like IOException, RecordStoreException etc... it will capture all, and you will not be stuck with any Error.
    Thanks,
    Ekta

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: how to use try/catch?

    You might like to read this and this.

    Catch exceptions only if you can do something useful with them. Otherwise, don't catch them.

    Graham.

Similar Threads

  1. [N95] Unhandled Exception caused by using try/catch
    By jeroenoosterlaar in forum Mobile Java General
    Replies: 7
    Last Post: 2010-08-18, 05:12
  2. Replies: 8
    Last Post: 2010-01-18, 06:31
  3. Catch error when try Sending MMS.
    By Dvirus in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2008-08-14, 12:36
  4. try catch and active objects
    By vishwas_vincy11 in forum Symbian
    Replies: 1
    Last Post: 2007-02-01, 18:12
  5. try catch finally dilema
    By swc43@blueyonder.co.uk in forum Mobile Java General
    Replies: 1
    Last Post: 2005-02-03, 12:20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×