How do I consolidate errors when catching? (IE. single catch clause)

Hey all,

I'm trying to create a simple exception handler class. I'm trying to make calls to

a database and run a querry but exceptions a, b, c, and d must be caught.

The only way I know to do this is with multiple catch statements like this.try{

connectDatabase();

runQuery();

}

catch(SQLException err){

system.out.println(err);

}

catch(IllegalAccessException err){

system.out.println(err);

}

catch(ClassNotFoundException err){

system.out.println(err);

}

catch(InstantiationException err){

system.out.println(err);

}

This gets very redundant in my code because all I want to do is call two methods

and I end up writing 20 lines of code. I was told that I can create a custom exception

object that somehow contains exceptions a, b, c, and d, but I do not know how to do this.

I would like to be able to do something like thistry{

connectDatabase();

runQuery();}

catch(GeneralError err){

system.out.println(err);}

Can someone please tell me what I will need in my GeneralError class?

So far, I've come as far as extending the Exception class.

Thanks for any help

[1845 byte] By [griffindja] at [2007-10-2 17:23:04]
# 1
You could usecatch (Exception ex){...}But that would be a good idea only if you want to handle all errors the same way.
Dick_Adamsa at 2007-7-13 18:39:15 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

Thanks.

That definately cleans the code up. But is this way acceptable or is it known as bad practice?

In this scenario that I asked about, I DO want to handle all excpetions the same way.

Specifically I will be passing the exception to a servlet and using that servlet to format the Exception and display the

Cause and StackTrace(same that tomcat does by default) but be able to put my websites template on it and format the text and such.

But for future knowledge, is there a way to create a class containing the 4 exceptions above,

so I can call exception specific methods like getSQLState() for SQLException?

Thanks for the reply.

griffindja at 2007-7-13 18:39:15 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...