I have a java app that try to insert a row into the table and com.ibatis.common.jdbc.exception.NestedSQLException is thrown with the Cause com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException
When I try to insert dublicate data for a unique-key constraint.
How do I catch that exception?
To get to the root cause you can do something like this:
try {
//insert
} catch (NestedSQLException e) {
Throwable t = e;
while(t.getCause() != null) {
t = t.getCause();
}
//in your situation, now t should be MySQLIntegrityConstraintViolationException
if (t instanceOf MySQLIntegrityConstraintViolationException) {
//do something
}
}
In case it helps someone. #tibtof's is correct and got me to:
public int insert(MyObject myObject) {
int recCount = -1;
try {
recCount = insert(myObject, MyObjectMapper.class);
} catch (Throwable e) {
Throwable t = e;
while (t.getCause() != null) {
t = t.getCause();
if (t instanceof SQLIntegrityConstraintViolationException) {
// get out gracefully.
recCount = -1;
return recCount;
}
}
//Something else wicked wrong happened.
LogUtils.error(log, e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
return webGroup.getWebGroupId().intValue();
}
Related
I was wondering, when exception occurs how try with resource statement manages to close resources before entering catch block.
When exception occurs execution immediately jumps to catch block. So where actually try-with-resource closes the resources.
To get better understanding of how it works I decided to see how compiler implements it. I wrote following code and and compiled it.
public class Test
{
public static void main(final String[] args) {
//I used same JDK for compilation and execution.
System.out.println("Java version: " + System.getProperty("java.version") + "\n");
try(CloseMe me = new CloseMe();
CloseMeToo meToo = new CloseMeToo()){
System.out.println("trying");
throw new Exception("try failed");
} catch(Exception e) {
System.out.println("failed");
System.out.println("\n");
System.out.println(e.getMessage());
System.out.println(e.getSuppressed()[0].getMessage());
System.out.println(e.getSuppressed()[1].getMessage());
}
}
}
class CloseMe implements AutoCloseable {
#Override
public void close() throws Exception {
System.out.println("me closing!");
throw new Exception("don't close me :o");
}
}
class CloseMeToo implements AutoCloseable {
#Override
public void close() throws Exception {
System.out.println("meToo closing!");
throw new Exception("don't close me too :O");
}
}
Output
Java version: 15.0.1
trying
meToo closing!
me closing!
failed
try failed
don't close me too :O
don't close me :o
Then I went to www.javadecompilers.com and tried the decompilers there. Two decompilers gave decent results: CFR 0.150 and Fernflower.
CFR is most readable and complete so posting it here.
public class Test
{
public static void main(final String[] args) throws Throwable{
System.out.println("Java version: " + System.getProperty("java.version") + "\n");
try {
Throwable throwable = null;
Object var2_4 = null; //<-- where this variable is used?
try {
CloseMe me = new CloseMe();
try {
CloseMeToo meToo = new CloseMeToo();
try {
System.out.println("trying");
throw new Exception("try failed");
}
catch (Throwable throwable2) {
throwable = throwable2; //<-- I put this line to make it work
if (meToo != null) {
meToo.close();
}
throw throwable2;
}
}
catch (Throwable throwable3) {
if (throwable == null) {
throwable = throwable3;
} else if (throwable != throwable3) {
throwable.addSuppressed(throwable3);
}
if (me != null) {
me.close();
}
throw throwable;
}
}
catch (Throwable throwable4) {
if (throwable == null) {
throwable = throwable4;
} else if (throwable != throwable4) {
throwable.addSuppressed(throwable4);
}
throw throwable;
}
}
catch (Exception e) {
System.out.println("failed");
System.out.println("\n");
System.out.println(e.getMessage());
System.out.println(e.getSuppressed()[0].getMessage());
System.out.println(e.getSuppressed()[1].getMessage());
}
}
}
I understand decompilers have limitations. Ideal decompiler would've given me same try-with-resource back and I wouldn't have seen these details. So it is ok.
My questions are:
In above decompiled code, variable var2_4 is unused. Also, I had to add a line to make it work like try-with-resource. I think the code is not complete. If there are any, can you add/explain missing parts?
If anyone, who understands bytecode, translate class file to exact java code would be great. Or give me pointers about where can I get the tools to do the job.
Thank you!
The behavior of try-with-resources is fully documented in the Java Language Specification, section 14.20.3. try-with-resources.
It specifically shows that the following abbreviated version of the question code:
try (CloseMe me = new CloseMe(); CloseMeToo meToo = new CloseMeToo()) {
System.out.println("trying");
} catch (Exception e) {
System.out.println("failed");
}
first gets converted to:
try {
try (CloseMe me = new CloseMe(); CloseMeToo meToo = new CloseMeToo()) {
System.out.println("trying");
}
} catch (Exception e) {
System.out.println("failed");
}
then to:
try {
final CloseMe me = new CloseMe();
Throwable #primaryExc1 = null;
try (CloseMeToo meToo = new CloseMeToo()) {
System.out.println("trying");
} catch (Throwable #t) {
#primaryExc1 = #t;
throw #t;
} finally {
if (me != null) {
if (#primaryExc1 != null) {
try {
me.close();
} catch (Throwable #suppressedExc) {
#primaryExc1.addSuppressed(#suppressedExc);
}
} else {
me.close();
}
}
}
} catch (Exception e) {
System.out.println("failed");
}
then to:
try {
final CloseMe me = new CloseMe();
Throwable #primaryExc1 = null;
try {
final CloseMeToo meToo = new CloseMeToo()
Throwable #primaryExc2 = null;
try {
System.out.println("trying");
catch (Throwable #t) {
#primaryExc2 = #t;
throw #t;
} finally {
if (meToo != null) {
if (#primaryExc2 != null) {
try {
meToo.close();
} catch (Throwable #suppressedExc) {
#primaryExc2.addSuppressed(#suppressedExc);
}
} else {
meToo.close();
}
}
}
} catch (Throwable #t) {
#primaryExc1 = #t;
throw #t;
} finally {
if (me != null) {
if (#primaryExc1 != null) {
try {
me.close();
} catch (Throwable #suppressedExc) {
#primaryExc1.addSuppressed(#suppressedExc);
}
} else {
me.close();
}
}
}
} catch (Exception e) {
System.out.println("failed");
}
I written a method which will acknowledge the controller by returning true and false, I return true inside try if everything goes fine it will return true and I return false inside catch blocks, but still method shows me error "missing return statement" what is the best way to do it.
The below method written in java will send back the true or false to the controller.
Secondly I want to carry the exception message from here to controller, I think of returning string, is it good approach,
Kindly suggest me the best way to do the exception handling
public boolean pickSalayData(String yearMonth, String regionId, String circleId, Userdetail loginUser) throws MyExceptionHandler {
String tableSuffix = yearMonth.substring(4, 6) + yearMonth.substring(0, 4);
log.info("Pick Salary Data From ERP " + DateUtility.dateToStringDDMMMYYYY(new Date()));
List<SalaryDetailReport> detailReports = hRMSPickSalaryDataDAO.findAll(yearMonth, regionId, circleId);
TransactionDefinition def = new DefaultTransactionDefinition();
TransactionStatus trstatus = transactionManager.getTransaction(def);
try {
List<SalaryDetailReport> salaryDetailReport = null;
int countDetail = 0;
if (detailReports != null && detailReports.size() > 0) {
for (SalaryDetailReport salary : detailReports) {
try {
if (countDetail % COMMIT_COUNT == 0) {
if (salaryDetailReport != null) {
salaryDetailReportDAO.save(salaryDetailReport, tableSuffix);
reportHistoryDAO.save(salaryDetailReport, loginUser);
}
salaryDetailReport = new ArrayList<SalaryDetailReport>();
}
salaryDetailReport.add(salary);
countDetail++;
} catch (Exception e) {
log.error("Error on Save Salary Pay Head Details Data from ERP to Prayas .");
}
}
if (salaryDetailReport != null && salaryDetailReport.size() > 0) {
salaryDetailReportDAO.save(salaryDetailReport, tableSuffix);
reportHistoryDAO.save(salaryDetailReport, loginUser);
}
} else {
throw new MyExceptionHandler("No record for Save in Database from ERP.");
}
salaryDetailReportDAO.update(tableSuffix, regionId, circleId);
List<SalaryDetailReport> reports = salaryDetailReportDAO.findAll(tableSuffix, regionId, circleId);
if (reports != null && reports.size() > 0) {
for (SalaryDetailReport salaryDetail : reports) {
try {
SalaryDetail sd = new SalaryDetail();
sd.setDetailReport(salaryDetail);
salaryDetailDAO.save(sd, tableSuffix);
} catch (Exception e) {
log.error("Error occured", e);
e.printStackTrace();
throw new MyExceptionHandler(" Error :" + e.getMessage());
}
}
System.out.println("data found");
} else {
log.error("Salary Record Not Found.");
throw new MyExceptionHandler("No record Found.");
}
salaryDetailDAO.updateEarningDeduction(tableSuffix);
//salaryDetailDAO.updateEarningDeductionsInSDT();
transactionManager.commit(trstatus);
try {
hRMSPickSalaryDataDAO.update(regionId, circleId, yearMonth);
return true;
} catch (Exception ex) {
log.error("Some error : ", ex);
}
// // System.out.println("Completed =============================");
} catch (MyExceptionHandler ex) {
transactionManager.rollback(trstatus);
ex.printStackTrace();
log.error("Failed to Save Salary data :" + ex.getMessage());
return false;
} catch (Exception ex) {
transactionManager.rollback(trstatus);
ex.printStackTrace();
log.error("Error occured on Save Salary data.", ex);
return false;
}
}
You are missing return statement for the following catch block :
catch (Exception ex) {
log.error("Some error : ", ex);
}
Either you add return statement in this catch block or at the end of mehtod
If this code throws an Exception, then the following catch code will not be entered into and hence there is no return value
try {
hRMSPickSalaryDataDAO.update(regionId, circleId, yearMonth);
return true;
} catch (Exception ex) {
log.error("Some error : ", ex);
**edit**
return `true||false`;
}
} catch (...) {
return something;
}
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.
im trying to implement SQLGrammarException into my method.
This method show me column error, but i need to show what procedure the column with error from.
public static PersistenceMicrodataException dealHibernateException(Throwable e) {
e.printStackTrace();
Throwable t = ExceptionUtil.getCause(e);
return new PersistenceMicrodataException(t.getMessage(), t);
}
I try this:
public static PersistenceMicrodataException dealHibernateException(Throwable e) {
try {
Throwable t = ExceptionUtil.getCause(e);
} catch (Exception e) {
System.out.println(t.getMessage());
System.out.println(((SQLGrammarException) t).getSQLState());
System.out.println(((SQLGrammarException) t).getErrorCode());
System.out.println(t.getCause());
}
return new PersistenceMicrodataException(e.getMessage(), e);
}
Someone can help me with this?
I found solution!
public static PersistenceMicrodataException dealHibernateException(Throwable e) {
String concatError = ((SQLGrammarException) e).getSQL() + ((SQLGrammarException) e).getClass() + ((SQLGrammarException) e).getCause();
while (e != null) {
java.lang.System.out.println(concatError);
break;
}
Throwable t = ExceptionUtil.getCause(e);
return new PersistenceMicrodataException(concatError,t);
}
This code below is the method header and body, but I get the following error: no exception of type object can be thrown an exception type must be a subclass of Throwable. I'm attempting to execute this block of code: catch(Object object).
public void method15665(Class435 class435, int i) {
do {
try {
try {
byte[] is
= new byte[(int) class435.method7563(1085678935)];
int i_3_;
for (int i_4_ = 0; i_4_ < is.length; i_4_ += i_3_) {
i_3_ = class435.method7564(is, i_4_, is.length - i_4_,
(byte) -10);
if (i_3_ == -1)
throw new EOFException();
}
Class224_Sub8 class224_sub8 = new Class224_Sub8(is);
if ((class224_sub8.aByteArray8535.length
- class224_sub8.anInt8536 * 475822179)
< 1) {
try {
class435.method7572(-1683167102);
} catch (Exception exception) {
/* empty */
}
break;
}
int i_5_ = class224_sub8.method13859((short) -7287);
if (i_5_ < 0 || i_5_ > 1) {
try {
class435.method7572(-1683167102);
} catch (Exception exception) {
/* empty */
}
break;
}
if ((class224_sub8.aByteArray8535.length
- class224_sub8.anInt8536 * 475822179)
< 2) {
try {
class435.method7572(-1683167102);
} catch (Exception exception) {
/* empty */
}
break;
}
int i_6_ = class224_sub8.method13737(2071056893);
if ((class224_sub8.aByteArray8535.length
- 475822179 * class224_sub8.anInt8536)
< 6 * i_6_) {
try {
class435.method7572(-1683167102);
} catch (Exception exception) {
/* empty */
}
break;
}
for (int i_7_ = 0; i_7_ < i_6_; i_7_++) {
Class323 class323
= Class399.aClass195_Sub2_Sub1_5932
.method14614(class224_sub8, -2141543778);
if ((Class255.aClass255_3016
== (((Class173_Sub1) this).aClass255Array9960
[class323.anInt5015 * 1568411443]))
&& (Class399.aClass195_Sub2_Sub1_5932.method14624
(class323.anInt5015 * 1568411443, 82620551)
.aClass350_2171.method6687
(-1035085164).aClass5162.isAssignableFrom
(class323.anObject5014.getClass())))
anInterface50_2149.method298((class323.anInt5015
* 1568411443),
class323.anObject5014,
-1250481088);
}
} catch (Exception exception) {
try {
class435.method7572(-1683167102);
} catch (Exception exception_8_) {
exception = exception_8_;
}
break;
}
try {
class435.method7572(-1683167102);
} catch (Exception exception) {
/* empty */
}
} catch (Object object) {
try {
class435.method7572(-1683167102);
} catch (Exception exception) {
/* empty */
}
throw object;
}
} while (false);
}
Does anyone know how fix this? It would be very much appreciated!
replace
} catch (Object object) {
with
} catch (Throwable object) {
actually you don't want to catch Throwable, but probably Exception, RuntimeException or an even more specific class.
You can only catch what can be thrown (IS-A Throwable). Hence, the compiler complains when you try to catch an Object (because it doesn't extend Throwable).
catch (Object o) // Error: Object IS-NOT Throwable
Throwable is inherited by all types of Errors and Exceptions. But, we usually don't catch Errors because a program almost always cannot recover from it for example, an OutOfMemoryError. So, a catch (Throwable t) is not recommended.
When using catch (Exception e) you basically have a catch-all for any exception (checked or un-checked) that might get thrown during the run. To use or not to use a generic catch usually depends on what you're try block is trying to do. For example, when reading a file you would like to handle and respond to a FileNotFoundException differently than say an EOFException.
All exceptions and errors extend Throwable, only those can be thrown and caught.
You can do
try{
throw new Exception();
}catch(Exception e){
// something here
}catch(Throwable t){
// something here
}
When you are writing multiple catch blocks, then keep following points in mind
You cannot write a subclass type AFTER a superclass type. i.e. If you write catch(RuntimeException rt){} AFTER catch(Exception e){} then you will get compiler error that it is already caught.