Java 在Android应用程序中处理后台线程异常的最佳实践
我的后台线程中有多个try/catch块,用于初始化和加载数据/DB。 首先,我刚刚放置了Java 在Android应用程序中处理后台线程异常的最佳实践,java,android,multithreading,exception,background,Java,Android,Multithreading,Exception,Background,我的后台线程中有多个try/catch块,用于初始化和加载数据/DB。 首先,我刚刚放置了抛出新的RuntimeException(e)在捕获中,但我认为我应该处理它们,而不是仅仅扔它们,希望有人来处理。。在这种情况下,顺便说一下,应用程序将崩溃 处理这些情况(在后台线程中)的最佳实践是什么,尤其是在Android应用程序中 谢谢 编辑: private static class DatabaseLoaderHelper extends SQLiteOpenHelper { priva
抛出新的RuntimeException(e)代码>在捕获中,但我认为我应该处理它们,而不是仅仅扔它们,希望有人来处理。。在这种情况下,顺便说一下,应用程序将崩溃
处理这些情况(在后台线程中)的最佳实践是什么,尤其是在Android应用程序中
谢谢
编辑:
private static class DatabaseLoaderHelper extends SQLiteOpenHelper
{
private static final int DB_VERSION = 1;
private static String DB_PATH = "" ;
private DatabaseLoaderHelper( WeakReference<Context> contextWrapper )
{
super( contextWrapper.get(), DB_NAME , null, DB_VERSION );
DB_PATH = contextWrapper.get().getDatabasePath( DB_NAME ).getAbsolutePath();
boolean dataBaseExist = checkDatabaseExist( contextWrapper );
if ( !dataBaseExist )
{
try
{
copyDatabase( contextWrapper.get() );
}
catch ( IOException e )
{
throw new RuntimeException( e );
}
}
}
private void copyDatabase( Context context ) throws IOException
{
InputStream myInput = null;
String outFileName = null;
OutputStream myOutput = null;
try
{
myInput = context.getAssets().open( DB_NAME );
outFileName = DB_PATH;
myOutput = new FileOutputStream( outFileName );
byte[] buffer = new byte[ 1024 ];
int length;
while ( ( length = myInput.read( buffer ) ) > 0 )
{
myOutput.write( buffer, 0, length );
}
}
catch( IOException e )
{
}
finally
{
myOutput.flush();
myOutput.close();
myInput.close();
}
}
private boolean checkDatabaseExist( WeakReference<Context> contextWrapper )
{
File dbFile = new File( DB_PATH + DB_NAME );
if ( !dbFile.getParentFile().exists() )
{
dbFile.getParentFile().mkdir();
return false;
}
return dbFile.exists();
}
私有静态类DatabaseLoaderHelper扩展了SQLiteOpenHelper
{
私有静态最终int DB_版本=1;
私有静态字符串DB_PATH=“”;
专用数据库装入器帮助器(WeakReference contextWrapper)
{
super(contextWrapper.get(),DB_名称,null,DB_版本);
DB_PATH=contextWrapper.get().getDatabasePath(DB_NAME.getAbsolutePath();
布尔dataBaseExist=checkDatabaseExist(contextWrapper);
如果(!数据库存在)
{
尝试
{
copyDatabase(contextWrapper.get());
}
捕获(IOE异常)
{
抛出新的运行时异常(e);
}
}
}
私有void copyDatabase(上下文)引发IOException
{
InputStream myInput=null;
字符串outFileName=null;
OutputStream myOutput=null;
尝试
{
myInput=context.getAssets().open(DB_NAME);
outFileName=DB_路径;
myOutput=新文件输出流(outFileName);
字节[]缓冲区=新字节[1024];
整数长度;
而((长度=myInput.read(缓冲区))>0)
{
写入(缓冲区,0,长度);
}
}
捕获(IOE异常)
{
}
最后
{
myOutput.flush();
myOutput.close();
myInput.close();
}
}
私有布尔checkDatabaseExist(WeakReference contextWrapper)
{
File dbFile=新文件(DB_路径+DB_名称);
如果(!dbFile.getParentFile().exists())
{
dbFile.getParentFile().mkdir();
返回false;
}
返回dbFile.exists();
}
如果您使用的是AsyncTask,通常的做法是在doBackground()中捕获异常并将其分配给类成员异常变量,然后在onPostExecute()中处理它.这取决于异常的类型和可能导致异常发生的原因。确实没有通用的答案。如果您改变问题并提供更具体的信息,我们可能会建议一种适合这种情况的方法。例如,这只是在您自己的应用程序中,还是您希望第三方呼叫您的app/通过某些IPC机制(意图、绑定服务、内容提供者等)使用数据?当这些异常发生时,您是否有合适的回退?这些异常确实发生了吗?等等。我在帖子中添加了一个程序简介,其中有一些异常我需要以某种方式处理。它是SQLOpenHelper的子类,在doInBackground()中初始化和调用。谢谢。顺便说一句,我也不知道在最后关闭资源文件的地方该怎么办。例如,如果myOutput.close()将引发异常-myInput.close()将不会被调用…我如何解决这个问题?