Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getView() can not throw Exception

Tags:

java

android

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?

like image 653
outcast Avatar asked Dec 29 '22 02:12

outcast


1 Answers

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.

like image 169
Cheryl Simon Avatar answered Dec 30 '22 17:12

Cheryl Simon