GetView () cannot throw an exception

I am creating a SingleItemAdapter extends ArrayAdapter , this adapted adapter is used for ListView .

In this SingleItemAdapter , I do some database stuff, so I want to throw an exception in the getView() method, which is an initialized GUI.

But public View getView(int position,View convertView,ViewGroup parent) throws an exception will receive Exception Exception is not compatible with throws clause in ArrayAdapter.getView(int, View, ViewGroup)

ArrayAdapter, BaseAdapter, The adapter does not throw an exception, so why?

+4
source share
3 answers

There are two different types of exceptions in Java. Checked exceptions require that the exception be handled at compile time using a method declaration or a try / catch block. Runtime exceptions do not have this requirement.

You cannot add a new type of exception to the declaration of an overridden method, so you need to either catch the exception, or handle it internally, or use the exception at run time.

It looks like in your case, you probably want to catch the exceptions from the database and handle them well in your code. It is possible that if access to the database fails, you may display an error message explaining the problem.

If you just throw an exception at runtime, then the user is likely to get a forced close screen, which is a pretty bad choice for the UX perspective.

+6
source

You cannot throw an exception from the method that you override unless the override method throws an exception. You are breaking an override point. Instead of throwing an exception, you should handle it.

+4
source

The purpose of the ArrayAdapter is to adapt the array for viewing. It should already be extracted from the database. This does not make sense if populating the view throws an exception.

0
source

Source: https://habr.com/ru/post/1335018/


All Articles