I have to add try/catch in my Kotlin code while using Android Studio, but I do not understand how to add this. Below are the codes where I have to add try/catch.
I did not try anything yet, because I am totally confused where to apply try/catch.
1.
class SmsReceiver : BroadcastReceiver(){
override fun onReceive(context: Context?, intent: Intent) {
val extras = intent.extras
if(extras != null){
val sms: Array<Any> = extras.getString("pdus") as Array<Any>
for(i in sms.indices){
val format = extras.getString("format")
var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
SmsMessage.createFromPdu(sms[i] as ByteArray,format)
}else{
SmsMessage.createFromPdu(sms[i] as ByteArray)
}
var phoneNumber = smsMessage.originatingAddress
val messageText = smsMessage.messageBody.toString()
Toast.makeText(context,"$phoneNumber:(Private)\n" + "messageText: $messageText",Toast.LENGTH_SHORT).show()
}
}
}
}
class MainActivity :AppCompatActivity(){
private val requestReceiveSms: Int = 3
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.RECEIVE_SMS) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.RECEIVE_SMS),
requestReceiveSms)
}
}
}
I expected "SMS received Toast message", but I get "Unfortunately,Application is stopped" and crashes...
Use:
try {
if(extras != null) {
val sms: Array<Any> = extras.getString("pdus") as Array<Any>
for(i in sms.indices) {
val format = extras.getString("format")
var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
SmsMessage.createFromPdu(sms[i] as ByteArray, format)
}
else {
SmsMessage.createFromPdu(sms[i] as ByteArray)
}
var phoneNumber = smsMessage.originatingAddress
val messageText = smsMessage.messageBody.toString()
Toast.makeText(context, "$phoneNumber:(Private)\n" + "messageText: $messageText", Toast.LENGTH_SHORT).show()
}
}
}
catch (ex: Exception) {
// Your error handling code here
// Here, consider adding Log.e("SmsReceiver", ex.localizedMessage)
// This log statement simply prints errors to your android studio terminal and will help with debugging, alternatively leave it out
if (context != null) {
Toast.makeText(context!!, ex.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
You should be applying the try catch over code which can throw exceptions. For the code you posted, there are a few places where it could potentially crash, such as index out of bounds (sms[i]
) or if (extras.getString("pdus"
) fails to find this key, hence my solution wraps both of these in the same try catch. What you then do with the exception is up to you.
If you want to handle more specific exceptions, you can also do this:
try {
if(extras != null) {
val sms: Array<Any> = extras.getString("pdus") as Array<Any>
for(i in sms.indices) {
val format = extras.getString("format")
var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
SmsMessage.createFromPdu(sms[i] as ByteArray, format)
}
else {
SmsMessage.createFromPdu(sms[i] as ByteArray)
}
var phoneNumber = smsMessage.originatingAddress
val messageText = smsMessage.messageBody.toString()
Toast.makeText(context, "$phoneNumber:(Private)\n" + "messageText: $messageText", Toast.LENGTH_SHORT).show()
}
}
}
catch (indexOutOfBoundsException:IndexOutOfBoundsException) {
// Your error handling code here
}
catch (nullpointer : NullPointerException) {
// Your error handling code here
}