• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

when do I try... catch... ?

SelArom

Senior member
hi i'm making a class that has a dataset as a private member. I have a public function addRow in that class that adds a new row to the dataset. That function is called by the mainForm (gui interface) class to add a new row to the dataset, but I don't know where to put the try... catch to check for duplicate rows. should I put it in the interface class or the class that has the dataset in the addRow function? the catch will alert the user that the row already exists, so I'm thinking that code should be in the mainForm instead, is that right? if so, should I not put any exception catchin for this error in the addRow class (since it will be caught by the try/catch in the mainForm)?
 
try catch is used to prevent errors from occuring. you wouldnt use it to let a user know that a row already exists

try

something that might throw an error like divide by 0

catch
output any errors that are thrown.
 
This is purely from a Java programming perspective, but the concept should be the same.

Your try catch block will be in main, and your addRow method will throw the exception.

Although I don't know why you would necessarily use a try catch block in this situation.

I mean you can if you want, but in my experience, I only use try catch blocks in situations where otherwise the program would cease to function after the error is thrown.

EDIT: Read what you said in GoBadgrs post, so I stand by what I said before.
 
Originally posted by: Gobadgrs
try catch is used to prevent errors from occuring. you wouldnt use it to let a user know that a row already exists

try

something that might throw an error like divide by 0

catch
output any errors that are thrown.

trying to add a duplicate row to a dataset (if it has a primary key, which it does, i should have mentioned this) will throw an exception, data.integrityexception or something like that... I need to know when to implement the catching in the class or in the interace.
 
Back
Top