phone stops on exception

  • Replies:1
  • OpenNot stickiedUnanswered

May 1, 2011 2:59:04 AM via Website

im developing my first application with android and im having the following trouble
in my project (which i created with eclipse) i throw an exception which is caught
but my phone will always stop and i get the message that the application stopped unexpectedly, like so

1protected void createList(String name, String srcLanguage, String trgLanguage) {
2 DbAdapter mDbHelper = new DbAdapter();
3 mDbHelper.open();
4 boolean success = false;
5 try {
6 mDbHelper.createList(name, srcLanguage, trgLanguage);
7 success = true;
8 } catch (ListExistsException lee) {
9 //sad but true...
10 }
11 mDbHelper.close();
12 if (success) {
13 this.finish();
14 } else {
15 Resources res = this.getResources();
16 Toast.makeText(this, res.getString(R.string.list_exists), Toast.LENGTH_SHORT).show();
17 }
18 }

and then


1public class DbAdapter {
2
3 public void open() {
4 // TODO Auto-generated method stub
5
6 }
7
8 public void createList(String name, String srcLanguage, String trgLanguage) throws ListExistsException {
9 // TODO Auto-generated method stub
10 throw new ListExistsException();
11 }
12
13 public void close() {
14 // TODO Auto-generated method stub
15
16 }
17
18}


1public class ListExistsException extends Exception {
2
3 /**
4 *
5 */
6 private static final long serialVersionUID = 1L;
7
8 @Override
9 public String getMessage() {
10 // TODO Auto-generated method stub
11 //return "List exists already";
12 return super.getMessage();
13 }
14
15}

whats wrong with this?

May 1, 2011 2:23:38 PM via Website

Hello Martin,

first of all, welcome on board ;)

It's allways "Best practice" to embedd logging capabilitys to your applications. Second .. Never ever leave a catch block empty like you did in your code. Doing so you will never ever see the reasons why something went wrong in your code.

Just implement something like :

Log.d("MyAppName", lee.toString());
inside your catch block.

Then you are able to see what happens if your code fails ...Watch the logcat output and tell us what you find in there .. normaly the exact line of code where the buggy thing resides is presented there.

lg Voss