简体   繁体   中英

getView() can not throw Exception

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.

You can't throw an exception from a method that you're overriding if the overriding method doesn't throw an exception. You're breaking the point of overriding. Instead of throwing an exception you have to handle it.

The purpose of the ArrayAdapter is to adapt the array for viewing. It should already have been fetched from the database. It does not make sense if the filling of the view throws an exception.

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