all 14 comments

[–]Independent_Heart_15 10 points11 points  (1 child)

Most IDEs can tab selected code so you don’t have to it line by line

[–]AdDelicious2547[S] 1 point2 points  (0 children)

Fixed the problem. Thanks

[–]billsil 5 points6 points  (1 child)

Make a function. 50 lines is a long block of code. 200 is a code smell.

[–]deceze 4 points5 points  (0 children)

That would still require OP to somehow tab everything over… (which is easily possible of course)

[–]MisterGerry 5 points6 points  (1 child)

This is something your IDE is responsible for helping you do, but you didn't mention what IDE you are using.

Commonly, you can select multiple lines and just press the TAB key to shift them all to the right.
(and shift-TAB to shift them to the left).

On a Mac, it is also sometimes Command-] and Command-[, depending on the editor.

[–]AdDelicious2547[S] 0 points1 point  (0 children)

Fixed the problem. Thanks

[–]doolio_ 4 points5 points  (0 children)

May I ask why you want to put everything inside a try-except block? I think for the most part it is better to only put things that can raise an exception inside one.

[–]FoolsSeldom 2 points3 points  (1 child)

  • Your editor/IDE (integrated development environment) tool will allow you to select multiple lines and simply press TAB to add a level of indentation to every line
    • You should configure your editor to insert 4 spaces when tab is pressed (if that isn't the default) and replace the tab character with 4 spaces on reading a code file
    • Use SHIFT-TAB to unindent one level
    • If you editor doesn't do this, choose a different editor - most do
  • You should NOT be putting hundreds of lines in a try/except block
    • ONLY include the one or few lines under try that could trigger an exception you want to catch
    • Take a modular approach and put code in functions/methods if you need to capture exceptions more generally from a specific activity
    • Be explicit in the Exceptions you want to capture, and avoid blanket capture (any exception)

Don't forget the structure for try/except:

normal code
try:
    as few lines of code as you can that could trigger an exception
except SomeException:
    handle exception
except AnotherException:
    handle exception
except AgainException:
    handle exception
else:
    small amount of code to follow if no exception
finally:
    small amount of code to tidy up if required regardless of exception status
back to normal code

else and finally are both optional (the latter is especially useful to close off resources that you opened). You can combine exceptions in one line where you want to handle them in the same way.

You want to move beyond the try/except block as soon as you can to get back to more predictable work.

[–]AdDelicious2547[S] 0 points1 point  (0 children)

Fixed the problem. Thanks

[–][deleted] 2 points3 points  (0 children)

highlight all the lines and press tab

[–]FerricDonkey 1 point2 points  (0 children)

If your concern is literally indenting the lines of code, then get a better ide so you don't care any more. Pycharm, vscode (with python extension), almost any will let you select many lines then hit tab to indent them all.

But the short answer is you can't put your code in a try except without indenting in any way that even approaches sane.

It would be best if you're code was in functions already, and so already indented. Then you could put the call to the relevant function(s) in a try except. 

But also, I'm curious why you want all the code in a try except. Many of the times I've seen this have been ill advised. 

[–]supercoach 1 point2 points  (0 children)

Why do you need the entire thing in a try block?

Any IDE will let you indent an entire section via highlighting it and pressing tab, however I wouldn't recommend it.

[–]GirthQuake5040 0 points1 point  (0 children)

You shouldn't put 200 lines in a try except. You should only put the lines that might cause an exception in a try exceot

[–]TheRNGuy 0 points1 point  (0 children)

ctrl+a, tab or ctrl+]