• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Exception handling in Core Springframework

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I have the following ApplicationContext in my program.

ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_XML);

SPRING_XML is a constant which contains a link to the file "Spring-All-Module.xml"

Contents of "Spring-All-Module.xml" are



contents of database.xml are :



Contents of mail.xml is




My Questions are:

1) If there is any exceptions let say : could not connect to database due to whatsoever reason we get an exception in database.xml then how to handle the exception in our code.

2) If there is any exception let say in mail.xml it could not connect to the mail server or host value is wrong or javaMailProperties are not correct or configurable then how to handle the exception in our code.

Please do guide as I have searched the net but couldn't get a suitable answer.

Thanks and Regards,
Sid.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to catch the exceptions yourself. Generally, most people design their application into layers. The service layer is responsible for the business logic. It is also repsonisble for catching exceptions and handling them. In most cases, exception handling might be merely logging the exception so someone can look at it.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jayesh A Lalwani wrote:You'll have to catch the exceptions yourself. Generally, most people design their application into layers. The service layer is responsible for the business logic. It is also repsonisble for catching exceptions and handling them. In most cases, exception handling might be merely logging the exception so someone can look at it.



Please could you give an example given the situation above. I am clueless. How to do the same in Springframework. Like if we get an exception in database.xml OR mail.xml then how do we handle the same after the following statement.

ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_XML);

do I write a try and catch block

try{
ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_XML);
} catch(Exception e) {
e.printstacktrace();
}


Please elaborate.

Thanks and Regards,
Sid.

 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't get any exceptions in xml files.

You get database exception in DAO and you can catch them in service layer (where you call DAO methods), as mentioned above, and catch DataAccessException.

Similarly you may get MailException in code where you are sending mail. You catch it there, log it, and take appropriate action (like passing a meaningful message to top layer (main method)).
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Surendra Kumar wrote:You don't get any exceptions in xml files.

You get database exception in DAO and you can catch them in service layer (where you call DAO methods), as mentioned above, and catch DataAccessException.

Similarly you may get MailException in code where you are sending mail. You catch it there, log it, and take appropriate action (like passing a meaningful message to top layer (main method)).



Hi Surendra,

I do understand that what you are trying to say.

But my question is a different one. With the line

ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_XML);

My both the XMLs database.xml and mail.xml would get instantiated and loaded into memory. If there is any error in instantiating the beans in database.xml or mail.xml then Spring would immediately throw an Exception and the program would stop. How does DAO layer come into picture here. DAO comes into picture later. If the XMLs throw exception then there should be some method to handle these exception rt. ?

Please help and guide me.

Thanks and Regards,
Sid.
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're right. You may get BeanCreationException if there is any problem in creating those beans, and you can catch that in the main method.

See this post.
 
Siddharth Bhargava
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Surendra Kumar wrote:Yes, you're right. You may get BeanCreationException if there is any problem in creating those beans, and you can catch that in the main method.

See this post.




Surendra How would I catch the exception in the main method.

Can I write

try{
ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_XML);
} catch(Exception e) {
e.printstacktrace();
}

in the main method.


I had seen the post which you gave but the user in that post had a genuine problem. He didn't have the jar on the classpath so he was having an issue.

In my case I am not having any issue but I am anticipating that if something goes wrong then how do I handle the exception ?

Please help.

Thanks and Regards,
Sid.
 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Siddharth Bhargava wrote:
try{
ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_XML);
} catch(Exception e) {
e.printstacktrace();
}



That works.

But those initialization exception are fatal and need not be checked.
You don't need to do that.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!