Thank U for all... On Tue, Feb 9, 2010 at 7:18 AM, Jason Proctor <jason.android.li...@gmail.com > wrote:
> indeed -- i did investigate moving to this pattern, but i found that > putting the assignment inside the try() allowed me to change my mind about > having a catch() clause too with no other code changes inside the method. > > i have wrappers for common items that are closable, such as streams, > readers/writers, cursors, etc, which check for null and swallow any > exceptions, so it's only one line inside the finally(). > > quite why stream.close() throws IOException is kinda beyond me though :-) > > > > > Good advice -- always be on the lookout for cleanup that can be done >> in a finally, even if you don't see it as strictly necessary! >> >> Actually, you can usually simplify this slightly: >> >> Cursor c = ...; >> try { >> dostuff(c); // Do something with the cursor >> } finally { >> c.close(); >> } >> >> There's no timing window or anything here. The variable 'c' won't be >> assigned unless there's a successful return of a cursor, and there's >> no opportunity for a throw between that and the operations on the >> cursor. >> >> Separating the assignment and moving it into the try body does require >> that you test for null in the finally clause. Occasionally it's worth >> the extra hassle, so you can combine the finally with another try/ >> catch -- but usually it's better to simply use a separate try/catch >> for each. In C++, assignments can generate exceptions, so doing the >> assignment within the try is a good habit, and sometimes essential. In >> Java, this is not a concern, so we can optimize for making it as easy >> as possible to write and read the code, and thus encouraging liberal >> use of the pattern! >> >> BTW, try/catch are not expensive in the non-throw case. It generates >> static code ranges that are looked up during handling throw, and >> doesn't involve any extra execution steps if no throw is done. So >> always just try for the cleanest code. >> >> On Feb 8, 10:12 am, Jason Proctor <jason.android.li...@gmail.com> >> wrote: >> >>> well, like the error says, the cursor is being garbage collected but >>> it hadn't been closed or deactivated. >>> >>> you must close your cursors! >>> >>> and do it in a finally block so that they still get closed even if >>> other code throws an exception. >>> >>> for example -- >>> >>> Cursor c = null; >>> >>> try >>> { >>> c = resolver.query (...); >>> >>> // ...run through the query} >>> >>> finally >>> { >>> if (c != null) >>> { >>> c.close (); >>> } >>> >>> } >>> >>> not exactly like this, but you get the idea. >>> >>> hth >>> J >>> >>> >>> >>> >>> >>> >Hi, >>> >>> >In my program i'm using sqlite database. >>> >>> >After using that Sqlite program when i'm using some other layout it >>> >is showing the below exception. >>> >>> >02-06 14:17:59.320: INFO/dalvikvm(853): Uncaught exception thrown by >>> >finalizer (will be discarded): >>> >02-06 14:17:59.371: INFO/dalvikvm(853): >>> >Ljava/lang/IllegalStateException;: Finalizing cursor >>> >android.database.sqlite.sqlitecur...@43c4e358 on DB1 that has not >>> >been deactivated or closed >>> >02-06 14:17:59.729: INFO/dalvikvm(853): at >>> >android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596) >>> >02-06 14:17:59.800: INFO/dalvikvm(853): at >>> >dalvik.system.NativeStart.run(Native Method) >>> >02-06 14:17:59.878: INFO/dalvikvm(853): Uncaught exception thrown by >>> >finalizer (will be discarded): >>> >02-06 14:17:59.900: INFO/dalvikvm(853): >>> >Ljava/lang/IllegalStateException;: Finalizing cursor >>> >android.database.sqlite.sqlitecur...@43b9cf30 on DB2 that has not >>> >been deactivated or closed >>> >02-06 14:17:59.909: INFO/dalvikvm(853): at >>> >android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596) >>> >02-06 14:17:59.919: INFO/dalvikvm(853): at >>> >dalvik.system.NativeStart.run(Native Method) >>> >02-06 14:17:59.968: INFO/dalvikvm(853): Uncaught exception thrown by >>> >finalizer (will be discarded): >>> >02-06 14:17:59.989: INFO/dalvikvm(853): >>> >Ljava/lang/IllegalStateException;: Finalizing cursor >>> >android.database.sqlite.sqlitecur...@43b9c5e8 on null that has not >>> >been deactivated or closed >>> >02-06 14:18:00.049: INFO/dalvikvm(853): at >>> >> > >android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596) >> >>> >02-06 14:18:00.049: INFO/dalvikvm(853): at >>> >dalvik.system.NativeStart.run(Native Method) >>> >02-06 14:18:00.088: INFO/dalvikvm(853): Uncaught exception thrown by >>> >finalizer (will be discarded): >>> >02-06 14:18:00.109: INFO/dalvikvm(853): >>> >Ljava/lang/IllegalStateException;: Finalizing cursor >>> >android.database.sqlite.sqlitecur...@43b95fc8 on null that has not >>> >been deactivated or closed >>> >02-06 14:18:00.119: INFO/dalvikvm(853): at >>> >android.database.sqlite.SQLiteCursor.finalize(SQLiteCursor.java:596) >>> >02-06 14:18:00.140: INFO/dalvikvm(853): at >>> >dalvik.system.NativeStart.run(Native Method) >>> >>> >wat's this?.. >>> >how to over come this? >>> >any one can explain me?.. >>> >>> >-- >>> >Thanks & Regards >>> >Sasikumar.S >>> >>> >-- >>> >You received this message because you are subscribed to the Google >>> >Groups "Android Developers" group. >>> >To post to this group, send email to >>> android-developers@googlegroups.com >>> >To unsubscribe from this group, send email to >>> >>> >android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com> >>> >For more options, visit this group at >>> ><http://groups.google.com/group/android-developers?hl=en> >>> http://groups.google.com/group/android-developers?hl=en >>> >>> -- >>> jason.vp.engineering.particle >>> >> >> -- >> You received this message because you are subscribed to the Google >> Groups "Android Developers" group. >> To post to this group, send email to android-developers@googlegroups.com >> To unsubscribe from this group, send email to >> android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com> >> For more options, visit this group at >> http://groups.google.com/group/android-developers?hl=en >> > > > -- > jason.vp.engineering.particle > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com> > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- Thanks & Regards Sasikumar.S -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en