Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Could someone help me understand this question?  RSS feed

 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm being asked to answer this question but have no idea how to and would like some advice:

Suppose I wrote the following method specification: public void manipulateData ( ) throws Java.sql.SQLException, Java.sql.SQLDataException {
You are writing code for a database program that will use this method and you want to handle each specifically. What should the try/catch clause look like? You can use no-ops-- empty blocks {}--for the catch clause contents. We are only interested in the syntax and structure of the statements here.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, do you know how try/catch blocks work? Oracle's website has tutorials on the matter: try and catch.
 
kenny gill
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just started learning them so I'm still very new to them. Thats why I was looking for advice.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try blocks exist so that if the program comes upon a line of code that would normally crash the program (cause an exception to be thrown), it can skip over that line. Catch blocks follow try blocks, and are executed if the code inside the try block was problematic and therefore skipped over. For example:



Another thing is that you can have multiple catch blocks after a try block like your assignment requires. In the above example, "ProblematicException" is the type of problem you're excepting to encounter and 'problem' is the name you've given to your exception object. Also, if the above program would encounter a problem within the try block other than "ProblematicException", it would still crash. The only problems it avoids are the ones specifically mentioned in the catch blocks.
I think you should be able to figure out how to complete your assignment from the information I've given you. Try it out and post back.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the program crashes because of an Exception, there is probably a good reason for crashing. The compiler insists certain types of Exception (checked) be handled. If the app crashes because of an Exception there may be an eror in your code allowing things like null to be propagated, or division by 0. Those things usually require you check the code and correct errors there.

Actually the app doesn’t crash. Only the current thread terminates, as you can see from the following Mickey‑Mouse program:-Now work out how to get your command line working again
 
Niraj Jha
Ranch Hand
Posts: 63
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try{

your code;

}catch(SQLDataException sqlde){

hadle;

}catch(SQLException sqle){

handle;

}

Hierarchy must be same, as SQLException is parent class of SQLDataException .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!