Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
should not be using finally
Source Link
Ryan Ische
  • 3.7k
  • 3
  • 24
  • 20

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally {  if (success) {   // equivalent of Python else goes here // putting this in a finally-block is especially // important if there is a return in the try-block } } 

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally {  if (success) {   // equivalent of Python else goes here // putting this in a finally-block is especially // important if there is a return in the try-block } } 

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } if (success) { // equivalent of Python else goes here } 
deleted 258 characters in body; edited body
Source Link
Ryan Ische
  • 3.7k
  • 3
  • 24
  • 20

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally { if (success) { // equivalent of Python else goes here    // putting this in a finally-block is especially // important if there is a return in the try-block  } } 

EDIT: I don't think the finally is really necessary (just the if statement), since the purpose of the finally block is to execute regardless of any exceptions being thrown in the try block. Since you only want the code to execute on success, you could just have the if statement occur after the try-catch block. Ultimately, it comes down to putting the right code in the try block.

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally { if (success) { // equivalent of Python else goes here } } 

EDIT: I don't think the finally is really necessary (just the if statement), since the purpose of the finally block is to execute regardless of any exceptions being thrown in the try block. Since you only want the code to execute on success, you could just have the if statement occur after the try-catch block. Ultimately, it comes down to putting the right code in the try block.

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally { if (success) { // equivalent of Python else goes here    // putting this in a finally-block is especially // important if there is a return in the try-block  } } 
added 24 characters in body
Source Link
Ryan Ische
  • 3.7k
  • 3
  • 24
  • 20

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally { if (success) { // equivalent of Python else goes here } } 

EDIT: I don't think the finally is really necessary (just the if statement), since the purpose of the finally block is to execute regardless of any exceptions being thrown in the try block. Since you only want the code to execute on success, you could just have the if statement occur after the try-catch block. Ultimately, it comes down to putting the right code in the try block.

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally { if (success) { // equivalent of Python else goes here } } 

EDIT: I don't think the finally is really necessary, since the purpose of the finally block is to execute regardless of any exceptions being thrown in the try block. Since you only want the code to execute on success, you could just have the if statement occur after the try-catch block. Ultimately, it comes down to putting the right code in the try block.

I'm not entirely convinced that I like it, but this would be equivalent of Python's else. It eliminates the problem's identified with putting the success code at the end of the try block.

bool success = true; try { something(); } catch (Exception e) { success = false; // other exception handling } finally { if (success) { // equivalent of Python else goes here } } 

EDIT: I don't think the finally is really necessary (just the if statement), since the purpose of the finally block is to execute regardless of any exceptions being thrown in the try block. Since you only want the code to execute on success, you could just have the if statement occur after the try-catch block. Ultimately, it comes down to putting the right code in the try block.

added my later thoughts on the finally block.
Source Link
Ryan Ische
  • 3.7k
  • 3
  • 24
  • 20
Loading
Source Link
Ryan Ische
  • 3.7k
  • 3
  • 24
  • 20
Loading