Not able to catch Custom Exception using Java -


i'm trying catch sql exception. i've 3 layers,

  1. controller 2. impl class 3. dao (there interface in flow too, on high level im putting 3 levels description).

controller

try {         // call interface in turn call impl     } catch (myexception e) {         logger.debug(e);     } catch(exception e) {                   logger.debug(e);     }         return null; 

impl

 try {         purchasedto = purdao.createpurchase(clientid);     } catch(myexception e) {  --> should catch here, i'm throwing myexception in dao         throw e;     }catch(exception e) { --> dao exception being catch here         throw e;     } 

dao

 try {           // business logic goes here          } catch (sqlexception e) {                     throw new myexception (e.geterrorcode(), e.getmessage()); --> catching here, here should go impl catch block of myexception      } catch (exception e) {                     e.printstacktrace();     } {         pt.close();         try{                             if (con != null)                 con.close();         } catch (sqlexception se) {             se.printstacktrace();         }     } 

my exception

public class myexception extends exception {  /**  *   */ private int errorcode; private string errordesc;   public int geterrorcode() {     return errorcode; }  public void seterrorcode(int errorcode) {     this.errorcode = errorcode; }  public string geterrordesc() {     return errordesc; }  public void seterrordesc(string errordesc) {     this.errordesc = errordesc; }  private static final long serialversionuid = 1l;  public myexception () {     super(); }  public myexception (int errorcode, string errordesc) {     super();     this.errorcode = errorcode;     this.errordesc = errordesc; }    } 

i'm getting sql exception in dao layer, i'm throwing custom exception if sql exception. when goes impl, goes normal exception catch block(it gets null pointer exception message). ideally should go custom exception catch, right? i'm doing wrong. please rectify me.

any ideas appreciated.

most scenario line pt.close(); in dao class throws exception. in case, because exception happens in block, exception not thrown. scenario explained here in more detail: https://stackoverflow.com/a/4264937/6169266


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -