Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

how to throw an exception

I am trying to do this

if(blnEmpty == true){
  throw new RuntimeException("Table missing Id for site " + m_siteId + " and source id A");
}

but the problem is it continuous to execute the code after this. I tried to throw new Exception() but same behavior. I want it to throw some sort of an error so that I know something went wrong. Also I don't want the app to proceed any further.

this method is prob 10th on the stack trace list so throwing it up all the way it a little difficult.

right now I have a method A which calls method B within a try catch block. method B throws an exception. method A doesn't seem to be catching the exception. when i did this the log.error message is no where to be found, but I can see the stack trace generated by the throw new Exception or RuntimeException

try{
  params.addNoWrap( getAllTermId (m_packageId, conn));
}catch (Exception e){
  log.error("Insert into mp_school_term_rfc a new row with siteid and SchoolTermID = A");
}

if(blnEmpty == true){
  throw new Exception();
}
0
muthiahmerchant
Asked:
muthiahmerchant
1 Solution
 
objectsCommented:
That looks fine, sounds more like that line is not being called.

Are you throwing that exception in addNoWrap()?

Try adding some logging before the exception is thrown to see what s happening

log.debug("bInEmpty="+bInEmpty);
if(blnEmpty == true){
   log.debug("About to throw exception");
  throw new RuntimeException("Table missing Id for site " + m_siteId + " and source id A");
}

0
 
JoeCommented:
You don't have the throw inside a try{ }catch{ with code after the try catch do you?

The throw should not be inside a catch block, or you need to re throw it in the catch if that is the desired operation.


Joe

0
 
WebstormCommented:
Hi muthiahmerchant,

try to print the error to the console, before using log because you may have a log file error :

try{
  params.addNoWrap( getAllTermId (m_packageId, conn));
}catch (Exception e){
  System.err.println("params.addNoWrap has thrown an exception :");
  e.printStackTrace();
  log.error("Insert into mp_school_term_rfc a new row with siteid and SchoolTermID = A");
}

You can also display the value of blnEmpty before testing it :

System.err.println("blnEmpty = "+blnEmpty);
if (blnEmpty) // or  if (blnEmpty == true)   or    if ((blnEmpty == true) == true) ...
{
  throw new RuntimeException("Table missing Id for site " + m_siteId + " and source id A");
}


0
 
muthiahmerchantAuthor Commented:
Thanks for all the help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now