Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 What is the recommended way of handling exceptions?
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
BigBear




PostPosted: Mon Jan 14, 2013 4:37 pm   Post subject: What is the recommended way of handling exceptions?

If you are executing multiple statements in a row that can throw an exception should you use two try-catch blocks or one and handle the exception from each line separably.

Option #1

code:

try{
    onething
}
catch{...}

try{
    anotherthing
}
catch{...}


Or option #1

code:

try{
    onething
    anothetring
}
catch onething's exception type{

}
catch anotherthing's exception type{

}

Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Mon Jan 14, 2013 5:03 pm   Post subject: RE:What is the recommended way of handling exceptions?

This largely depends if you want "anotherthing" to be attempted or not, given that "onething" is throwing an exception.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Mon Jan 14, 2013 10:24 pm   Post subject: RE:What is the recommended way of handling exceptions?

There's no right answer. A lot of it comes down to how it "feels."

For me, the question is whether onething and anotherthing are related. If so, then they should probably be in the same try block.
BigBear




PostPosted: Tue Jan 15, 2013 11:20 am   Post subject: RE:What is the recommended way of handling exceptions?

Is it recommended to have the exception type even if you just abort and don't use it?
Tony




PostPosted: Tue Jan 15, 2013 1:08 pm   Post subject: RE:What is the recommended way of handling exceptions?

It is recommended to have the exception type so that you are catching only the expected exceptions. Should things truly go wrong, you'd want to know about this.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: