简体   繁体   中英

How to Toast the Exception message

I am creating one android application in which i am trying to insert the values in sqlite database but When i try to insert wrong value it display in terminal android.database.sqlite.SQLiteConstraintException: CHECK constraint failed: but i want to toast one message . I have used try-catch block but it doesn't working.

        registerbtn.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v)  {

                 if (isAllFieldsChecked = CheckAllFields()) {
            try {
                  dbHandler.insertValue(name.getText().toString(),
                       Age.getText().toString(), gender1.trim(),
                       email.getText().toString(), username.getText().toString(),
                       password.getText().toString(), contactno.getText().toString(),
                       Address.getText().toString(), city.getText().toString(),
                       encodedImage, item, uid.getText().toString());
                         Toast.makeText(getApplicationContext(), "Registration successful.", Toast.LENGTH_LONG).show();
                          textToSpeech.speak("Registration successful.",TextToSpeech.QUEUE_FLUSH,null);
                           Intent i = new Intent(RegisterActivity.this, MainActivity.class);
                           i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                           startActivity(i);
}
                       
                    catch (SQLiteConstraintException e) {
                                 final TextView textView=findViewById(R.id.bloodgroup6);
                                final ScrollView sc = findViewById(R.id.scroll);
                                    sc.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            sc.scrollTo(0,city.getTop());
                                        }
                                    });

                                 textToSpeech.speak("please select blood group",TextToSpeech.QUEUE_FLUSH,null);
                           Toast.makeText(getApplicationContext(), "please select blood group", Toast.LENGTH_SHORT).show();
                     }

                 }

            }
        });

This exception getting in terminal but I want to catch that exception in try catch block:-

E/SQLiteDatabase: Error inserting Uid=334 password=4567 img=null bloodgrp=select blood Email=ffg gender=male Address=gg Username=rushikesh city=ouh age=55 Name=ggg contactNo=6699588855
    android.database.sqlite.SQLiteConstraintException: CHECK constraint failed: register (code 275)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:783)
        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1560)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1429)
        at com.example.software2.bloodbankmanagement.DBHandler.insertValue(DBHandler.java:107)
        at com.example.software2.bloodbankmanagement.RegisterActivity$19.onClick(RegisterActivity.java:384)

if you wanna show the exception message on the toast, replace:

Toast.makeText(getApplicationContext(), "please select blood group", Toast.LENGTH_SHORT).show();

with:

Toast.makeText(getApplicationContext(), e.getMessage() , Toast.LENGTH_SHORT).show();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM