如何将 try-catch 包含在公共方法中并在需要时调用。
我在一个屏幕代码中尝试捕获。我想通过调用一个公开的方法(对整个应用程序)从另一个屏幕调用它。
可能吗 ?如果有怎么办。
请指导。
重新编辑:
如以下代码所示,第二个选项卡窗格实现已显示,请忽略与本机 java 的语法差异(这已为 Blackberry JDE 实现)。实现结构保持不变,因此请忽略差异并为所面临的问题提出合乎逻辑的解决方案。
// setup the second tab
vfm = new VerticalFieldManager(
Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH |
Manager.VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL );
//Initialize grid for publishing results
grid.add(new LabelField("Name")
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.CYAN);
super.paint(graphics);
}
});
grid.add(new LabelField("Total")
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.CYAN);
super.paint(graphics);
}
});
grid.setColumnPadding(100);
grid.setRowPadding(20);
//TRY CATCH STARTS HERE
try
{
//Open or create the database
Database db = DatabaseFactory.openOrCreate("database1.db");
Statement statementG55 = db.createStatement("CREATE TABLE IF NOT EXISTS GTemp4(gname TEXT,gbal INTEGER)");
statementG55.prepare();
statementG55.execute();
statementG55.close();
Statement statementG56 = db.createStatement("SELECT gname,gbal FROM GTemp4 ORDER BY ROWID DESC");
statementG56.prepare();
statementG56.execute();
Cursor c = statementG56.getCursor();
//Get to the row of grid
for (int i =1; i < grid.getRowCount(); i++)
{
System.out.println("Inside for first loops");
//Get to the column of grid
for (int j = 0; j < grid.getColumnCount() ; j++)
{
System.out.println("Inside for second loops");
//Get to the row of temp4 table
while(c.next())
{
System.out.println("Inside while");
Row r;
r = c.getRow();
for (int k = 1; k >=0; k--)
{
System.out.println("Inside for loops");
if(k==0)
{
System.out.println("Retrieving Names");
grid.insert(new LabelField(r.getString(k))
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
}
else
{
System.out.println("Retrieving other values");
String p = "" + r.getObject(k);
grid.insert(new LabelField(p)
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
}
grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.MIDNIGHTBLUE,Color.STEELBLUE,Color.MIDNIGHTBLUE,Color.STEELBLUE));
}
System.out.println("Exiting while");
}
System.out.println("Exiting sec for");
break;
}
System.out.println("Exiting first for");
break;
}
statementG56.close();
db.close();
}
catch(Exception e)
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
vfm.add(grid);
nullFld = new NullField( Field.FOCUSABLE );
hfm = new HorizontalFieldManager();
hfm.add( nullFld );
hfm.add( myLbl );
pane = new Pane( hfm, vfm );
model.addPane( pane );
非常感谢下面所有提出建议的人。