Basically, I've 2 adapter class, one is MyAdapter class and another is BaseRecyclerViewAdapter.
So, MyAdapter extends BaseRecyclerViewAdapter
BaseRecyclerViewAdapter
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
abstract class BaseRecyclerViewAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
abstract fun onCreateBaseViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder
abstract fun onBindBaseViewHolder(viewHolder: RecyclerView.ViewHolder, viewType: Int)
abstract fun getBaseItemCount(): Int
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return onCreateBaseViewHolder(parent, viewType)
}
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, viewType: Int) {
try {
onBindBaseViewHolder(viewHolder, viewType)
} catch (e: Exception) {
e.printStackTrace()
}
}
override fun getItemCount(): Int {
return getBaseItemCount()
}
}
MyAdapter:
class MyAdapter: BaseRecyclerViewAdapter {
...
override fun onBindBaseViewHolder(viewHolder: RecyclerView.ViewHolder, viewType: Int) {
// manually creating to exception for testing purpose
val test = ""
val count = test.get(5)
}
......
}
Now, inside MyAdapter class, I extended BaseRecyclerViewAdapter and overridden 3 methods, and written all logic. Now inside, (MyAdapter -> onBindBaseViewHolder) if any exception is coming then ideally it should catch inside (BaseRecyclerViewAdapter -> onBindViewHolder) but it is still going here (MyAdapter -> onBindBaseViewHolder)
EDIT: No try catch block exist inside MyAdapter - onBindBaseViewHolder, And I don't want that. See I've 50 adapter classes, and I want all route through this base class and if exception comes then should catch from base class, so that I don't need to add from every class.
CodePudding user response:
Check it out:
override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, viewType: Int) {
try {
test(viewHolder, viewType)
} catch (e: Exception) {
LogUtils.d("Hello print")
e.printStackTrace()
}
}
override fun getItemCount(): Int {
return getBaseItemCount()
}
fun test(viewHolder: RecyclerView.ViewHolder, viewType: Int) {
onBindBaseViewHolder(viewHolder, viewType)
}