Reputation: 197
I was testing some vulnerabilities inside of my code and trying to fix them by throwing an exception when the user has an invalid input. Now when I implement a try-catch and run the application on my phone, it crashes when I put in that invalid input.
I assume my code doesn't catch the exception from the addData method. Is there another way to implement exceptions or how can I make it possible to catch the exception from the addData method?
package com.odisee.photoboothapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.EditText;
import android.widget.Toast;
import com.odisee.photoboothapp.fontchanger.FontChangeTextView;
public class Form_Database extends AppCompatActivity {
DatabaseHelper myDb;
int selectedId;
RadioGroup test;
RadioButton editEducation;
EditText editName, editSurname, editEmail;
Button btnAddData;
@Override
protected void onCreate(Bundle savedInstanceState) throws IllegalArgumentException {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_form__database);
myDb = new DatabaseHelper(this);
test = (RadioGroup)findViewById(R.id.radioButtonChoice);
editName = (EditText)findViewById(R.id.edit_Name);
editSurname = (EditText)findViewById(R.id.edit_Surname);
editEmail = (EditText)findViewById(R.id.edit_Email);
btnAddData = (Button)findViewById(R.id.btnSend);
try {
addData();
}
catch(IllegalArgumentException e) {
Toast.makeText(Form_Database.this,"Data not inserted" + e.getMessage(),Toast.LENGTH_LONG).show();
}
}
public void addData() {
btnAddData.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(editName.getText().toString().contains("DROP")) {
throw new IllegalArgumentException("SQL Exceptie!");
}
else {
selectedId = test.getCheckedRadioButtonId();
editEducation = (RadioButton)findViewById(selectedId);
boolean isInserted = myDb.insertData(editName.getText().toString(), editSurname.getText().toString(), editEmail.getText().toString(), editEducation.getText().toString());
sendEmail();
if(isInserted == true) {
Toast.makeText(Form_Database.this,"Data inserted",Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(Form_Database.this,"Data not inserted",Toast.LENGTH_LONG).show();
}
}
}
}
);
}
public void sendEmail() {
//Getting content for email
String email = editEmail.getText().toString();
String subject = "testberichtje voor lorenzo";
String message = "testberichtje voor lorenzo";
//Creating SendMail object
SendMail sm = new SendMail(this, email, subject, message);
//Executing sendmail to send email
sm.execute();
}
}
05-01 17:58:17.021 30232-30232/com.odisee.photoboothapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.odisee.photoboothapp, PID: 30232 java.lang.IllegalArgumentException: SQL Exceptie! at com.odisee.photoboothapp.Form_Database$1.onClick(Form_Database.java:55) at android.view.View.performClick(View.java:5697) at android.widget.TextView.performClick(TextView.java:10826) at android.view.View$PerformClick.run(View.java:22526) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Upvotes: 1
Views: 19499
Reputation: 3783
Try making the catch more generic so you can actually catch the error, then print the stack trace so you can see the issue:
try{
//Try to do something on here
}catch(Exception error1) {
Log.e(TAG, "The exception caught while executing the process. (error1)")
error1.printStackTrace();
}
Upvotes: 5
Reputation: 1125
The problem is, you're not wrapping your exception prone code in the try-catch
clause. You've set an OnClickListener
to a Button
and this is the only part that's being tested.
The actual button presses are asynchronous, and they don't take place in the tested block of code.
Actually you don't need an exception here - exceptions should not be used to control normal application flow. In your case all you should do is to display a Toast
that explains to a user that they have used a forbidden keyword.
Upvotes: 2