I make a SingleItemAdapter
extends ArrayAdapter
, this customized Adapter is used for a ListView
.
In this SingleItemAdapter
, I do some database stuff, so I want to throw Exception in getView()
method which is initialized GUI.
But public View getView(int position,View convertView,ViewGroup parent)
throws Exception
will get Exception Exception is not compatible with throws clause in ArrayAdapter.getView(int, View, ViewGroup)
ArrayAdapter, BaseAdapter, Adapter does not throw Exception, so why?
There are two different types of Exceptions in Java. Checked exceptions require that the exception be handled at compile time via a method declaration, or a try/catch block. Runtime exceptions do not have this requirement.
You can't add a new type of Exception to an overridden method declaration, so you either need to catch the exception and handle it internally, or use a runtime exception.
It sounds like in your case you probably want to catch the database exceptions, and handle them nicely within your code. Perhaps, if the database access fails you can display an error message explaining the problem.
If you just throw a runtime exception, then the user is likely to get a force close screen, which is a pretty poor choice for a UX perspective.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With