I'm doing something like this:
try {
client.restoreFromClusterSnapshot(req);
} catch (AmazonRedshiftException e) {
txUtils.execute((ts) -> {
redshiftDto.setStatus(ResourceStatus.FAILED);
redshiftDto.setStatusDetails(e.getMessage());
redshiftDto.setUpdatedOn(Timestamp.from(Instant.now()));
this.rdao.merge(redshiftDto);
return null;
});
LOGGER.error("CANNOT START REDSHIFT- " + e.getErrorMessage());
throw new AmazonRedshiftException( "CANNOT START REDSHIFT- "
+ e.getErrorMessage());
}
In this code, I'm not able to set database variable if I'm throwing an error because it is terminating my transaction. If I'll comment that throw it will work and my database value will be set. But I'll not able to throw anything. How can I do both- (throwing and setting value in DB)
What I'd do is make use of the finally clause.
AmazonRedshiftException exception = null;
try {
cluster = client.restoreFromClusterSnapshot(req);
} catch (AmazonRedshiftException e) {
exception = e;
LOGGER.error("CANNOT START REDSHIFT- " + e.getErrorMessage());
throw new AmazonRedshiftException( "CANNOT START REDSHIFT- "
+ e.getErrorMessage());
} finally {
if(exception != null) {
txUtils.execute((ts) -> {
redshiftDto.setStatus(ResourceStatus.FAILED);
redshiftDto.setStatusDetails(exception.getMessage());
redshiftDto.setUpdatedOn(Timestamp.from(Instant.now()));
this.rdao.merge(redshiftDto);
return null;
});
}
}
A project source code has a Java method for SQL handling. The method does work, but it uses a questionable workaround: try-catch block at the very end of the method for normal execution. What is the correct way to implement it?
public void run() {
if (running) {
return;
}
running = true;
while(null == Common.server || null == Common.database || !ConnectionsPool.isInitialized()) {
// Wait until the database is set before continuing...
try {
Thread.sleep(1000);
}
catch(Exception ex) {}
}
while(running) {
final Connections cs = ConnectionsPool.getConnections();
Connection c = null;
while(!entries.isEmpty()) {
if (null == c) {
c = cs.getConnection();
}
SQLLogEntry entry = entries.remove();
if (null != entry) {
try {
write(entry, c); //find usages
}
catch (SQLException ex) {
writeLogFile("Could not write entry to SQL", ex);
}
}
}
if (null != c) {
try {
c.commit();
}
catch (SQLException ex) {
writeLogFile("Could commit to SQL", ex);
try {
c.rollback();
}
catch (SQLException ex1) {
}
// log
final StringWriter err = new StringWriter();
ex.printStackTrace(new PrintWriter(err));
EditorTransactionUtil.writeLogFile(err.toString());
// for user
final String msg = "Exception: " + EditorUtil.getErrorMessage(ex.getMessage());
try {
SwingUtilities.invokeAndWait(() -> {
JOptionPane.showMessageDialog(null, msg);
});
}
catch (Throwable ex1) {
}
}
finally {
cs.returnConnection(c);
}
c = null;
}
synchronized(entries) {
try {
entries.wait(1000);
}
catch (InterruptedException ex) {
// This is a workaround to process this loop...
}
}
}
writeLogFile("SQLMsgLogger run loop stopping...");
}
Problems with this code start here.
If(running) return;
running=true;
This is clearly an attempt to make sure that only one thread executes. This is a wrong way to check concurrency. Second tread might kick in right when if check ended, but assignment didn't start yet. You need to use syncronizible interface.
As for the disposed try catch block - as Konrad pointed out it will not be executed without Thread.interrupt() call. It might be dead code left from previous versions.
I have the methods:
public void sendTroops(ArrayList<Troops> troops){
try {
output.writeObject(troops);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendTowers(ArrayList<Tower> towers){
try {
output.writeObject(towers);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendString(ArrayList<String> str){
try {
output.writeObject(str);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
#SuppressWarnings("unchecked")
public ArrayList<Object> receiveObjects(){
try{
return (ArrayList<Object>) input.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
used here:
private void updateConnection(){
if(isClientActive()){
if(player.getType() == 0){
client.sendTowers(towers);
System.out.println("sent towers: " + towers.size());
}else{
client.sendTroops(troops);
System.out.println("sent troops: " + troops.size());
}
ArrayList<Object> obj = client.receiveObjects();
System.out.println("obj: " +obj.size());
if(obj != null && obj.size() > 0){
if (obj.get(0) instanceof Troops) {
mendTroops(obj);
}else if(obj.get(0) instanceof Troops){
mendTowers(obj);
}
}
}else if(isServerActive()){
if(player.getType() == 0){
server.sendTowers(towers);
System.out.println("sent towers: " + towers.size());
}else{
server.sendTroops(troops);
System.out.println("sent troops: " + troops.size());
}
ArrayList<Object> obj = server.receiveObjects();
System.out.println("obj: " +obj.size());
if(obj != null && obj.size() > 0){
if (obj.get(0) instanceof Troops) {
mendTroops(obj);
}else if(obj.get(0) instanceof Troops){
mendTowers(obj);
}
}
}
}
I was able to determine that the size of the object array from server/client.receiveObjects(); is always resulting in a array of size zero. I am unsure why this is the issue (as no errors are thrown). Either this is terrible coding practice (which it very well may be) or I'm over looking something.
I would appreciate any help, and if more code/ information of how this program works is needed, please let me know.
So what I want to do is when my Jsoup connection times out I want to bring up an alert dialog. Right now it does nothing. It just skips over there error and doesn't catch a timeout exception or crash. I'm new to java so I'm not sure how to catch this sockettimeoutexception and reroute it to another method. Can someone tell me how to go to another method when jsoup time out?
private void waterLevel() {
// TODO Auto-generated method stub
try {
levelDoc = Jsoup.connect("http://waterdata.usgs.gov/va/nwis/uv?site_no=02037500roop").timeout(3000).get();
} catch (SocketTimeoutException a) {
Log.e("MyAPP", "Exception----------A!", a);
a.printStackTrace();
alertdialog();
} catch (Exception e) {
Log.e("MyAPP", "Exception----------E!", e);
}
for (Element table : levelDoc.select("table[id=table_07_00065]")) {
String tableText = table.text();
depthArray = tableText.split(" ");
waterLevel = Double.parseDouble(depthArray[4]);
tvWaterLevel.setText(depthArray[4]+"FT");
if(waterLevel >= 5.0 && waterLevel < 9.0){
tvAlert.setText("LIFE JACKET REQUIRED");
}
else if (waterLevel >= 9.0){
tvAlert.setText("HIGH WATER PERMIT REQUIRED");
}
else{
tvAlert.setText("");
}
}
}
So I add changed the code to this and it gives me what i want:
private void waterLevel() {
// TODO Auto-generated method stub
try {
levelDoc = Jsoup.connect("http://waterdata.usgs.gov/va/nwis/uv?site_no=02037500").timeout(4000).get();
} catch (SocketTimeoutException a) {
Log.e("MyAPP", "Exception----------A!", a);
a.printStackTrace();
} catch (Exception e) {
Log.e("MyAPP", "Exception----------E!", e);
}
tvWaterLevel.setText("");
for (Element table : levelDoc.select("table[id=table_07_00065]")) {
String tableText = table.text();
depthArray = tableText.split(" ");
waterLevel = Double.parseDouble(depthArray[4]);
tvWaterLevel.setText(depthArray[4]+"FT");
if(waterLevel >= 5.0 && waterLevel < 9.0){
tvAlert.setText("LIFE JACKET REQUIRED");
}
else if (waterLevel >= 9.0){
tvAlert.setText("HIGH WATER PERMIT REQUIRED");
}
else{
tvAlert.setText("");
}
}
if (tvWaterLevel.length() < 1){
connectionAlarm();
}
}
I'd like to know how I could throw a "final" Exception, containing a detailed message with all the detailed messages of a number of chained exceptions.
For example suppose a code like this:
try {
try {
try {
try {
//Some error here
} catch (Exception e) {
throw new Exception("FIRST EXCEPTION", e);
}
} catch (Exception e) {
throw new Exception("SECOND EXCEPTION", e);
}
} catch (Exception e) {
throw new Exception("THIRD EXCEPTION", e);
}
} catch (Exception e) {
String allMessages = //all the messages
throw new Exception(allMessages, e);
}
I'm not interested in the full stackTrace, but only in the messages, I wrote. I mean, I'd like to have a result like this:
java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION
I think what you need is:
public static List<String> getExceptionMessageChain(Throwable throwable) {
List<String> result = new ArrayList<String>();
while (throwable != null) {
result.add(throwable.getMessage());
throwable = throwable.getCause();
}
return result; //["THIRD EXCEPTION", "SECOND EXCEPTION", "FIRST EXCEPTION"]
}
you can better use it this way, merge the message() of previous Exception with the message() of new Exception you are throwing:
} catch (Exception e) {
throw new Exception("FIRST EXCEPTION" + e.getMessage(), e);
}
Cycle through the exception cause and append the message in each exception.
try
{
try
{
try
{
try
{
throw new RuntimeException("Message");
}
catch (Exception e)
{
throw new Exception("FIRST EXCEPTION", e);
}
}
catch (Exception e)
{
throw new Exception("SECOND EXCEPTION", e);
}
}
catch (Exception e)
{
throw new Exception("THIRD EXCEPTION", e);
}
}
catch (Exception e)
{
String message = e.getMessage();
Throwable inner = null;
Throwable root = e;
while ((inner = root.getCause()) != null)
{
message += " " + inner.getMessage();
root = inner;
}
System.out.println(message);
}
Which prints
THIRD EXCEPTION SECOND EXCEPTION FIRST EXCEPTION Message
You can just add the previous exception message on each exception
This is an example :
public static void main(String[] args) {
try {
try {
try {
try {
throw new Exception();
// Some error here
} catch (Exception e) {
throw new Exception("FIRST EXCEPTION", e);
}
} catch (Exception e) {
Exception e2 = new Exception("SECOND EXCEPTION + " + e.getMessage());
throw e2;
}
} catch (Exception e) {
Exception e3 = new Exception("THIRD EXCEPTION + " + e.getMessage());
throw e3;
}
} catch (Exception e) {
System.out.println(e);
}
}
The result is : java.lang.Exception: THIRD EXCEPTION + SECOND EXCEPTION + FIRST EXCEPTION
Here is a nice utility for converting chained exceptions to string:
public final class ThrowableUtil {
private ThrowableUtil() {}
public static String chainedString(#NonNull Throwable throwable) {
StringBuilder SB = new StringBuilder(throwable.toString());
while((throwable = throwable.getCause()) != null)
SB.append("\ncaused by ").append(throwable);
return SB.toString();
}
public static String chainedString(#NonNull String msg, #NonNull Throwable throwable) {
StringBuilder SB = new StringBuilder(msg);
do {
SB.append("\ncaused by ").append(throwable);
} while((throwable = throwable.getCause()) != null);
return SB.toString();
}
}
Example output:
ThrowableUtil.chainedString(e);
produces
java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type
Another example output:
ThrowableUtil.chainedString("Writing of media file failed", e);
produces
Writing of media file failed
caused by java.io.IOException: Failed to create required video encoder
caused by java.lang.RuntimeException: Invalid mime type
I had saved all attributes in a class object with the following example:
public List<ErrorMessage> getMessageList(Throwable throwable) {
List<ErrorMessage> errorMessageList = new ArrayList<ErrorMessage>();
while (throwable != null) {
ErrorMessage message = new ErrorMessage();
message.set_message( throwable.getMessage());
message.set_line(throwable.getStackTrace()[0].getLineNumber());
message.set_methodName(throwable.getStackTrace()[0].getMethodName());
message.set_fileName(throwable.getStackTrace()[0].getFileName() );
message.set_className(throwable.getStackTrace()[0].getClassName());
errorMessageList.add(message);
throwable = throwable.getCause();
}
return errorMessageList;
}
Maybe simpler
try {
// code that throws exception
} catch(Throwable e ) {
var messages = new ArrayList<String>();
do {
messages.add(e.getMessage());
e = e.getCause();
} while( e!= null );
var message = String.join(" -> ", messages);
System.out.println(message);
}