DEVOPS 101 | PYTHON | EXCEPTION HANDLING

DEVOPS 101 | PYTHON | EXCEPTION HANDLING

NON-TECH PERSON EXPLANATION + TECH VERSION

==================================

?? PYTHON IS NOT DIFFICULT ??

?? First off, Python is not a snake.

The Python programming language has nothing to do with the slithering animal called ppython.

But I will tell you this: Python, as a programming language, is one of the simplest and easiest programming languages to learn.

If you’re dipping your toes into the vast ocean of programming, take my advice - start with Python.

It’s like the gateway drug to coding. Once you get the hang of it, venturing into the more complex territories like Java or C++ won’t feel so intimidating.

?? And if you’re eyeing a career in the DevOps realm, then you will have to start considering Python as your must-have sidekick.

Here’s a cool nugget about Python – it is very much like Microsoft Excel. They both have similar functions and operators. So, if you know Excel, then learning Python will be quite easy.

.

.

?? NON-TECH PERSON ANALOGY ??

THE TEACHER GUIDING HER STUDENTS

Picture yourself as a teacher doling out assignments to your students. You lay down the instructions, expecting everyone to follow suit. But challenges and slip-ups are bound to happen.

Thus, as a savvy teacher, you’re ready for them. You anticipate the hurdles your students will likely face and throw in some extra guidance for students who might hit a snag.

In the same vein, in the world of programming, when a computer programme takes the stage and starts running, it’s essentially following a script or a set of instructions prepared by the developer.

Sometimes, unexpected issues or errors occur while the programme is running. That’s where exception handling struts in, playing the guardian angel to your programme, just like you’d help a student tackle a tricky assignment.

Exception handling is like a set of guidelines you give your programme to deal with those unexpected issues. They ensure that your programme doesn’t throw a tantrum but can recover gracefully and continue its operation whenever an error arises.

Cool, right?

Now, let’s dive into the tech talk.

.

.

?? LETS GET TECHY ??

Exception Handling is an indispensable facet of error management within Python and programming in general. Let’s use a simple example to explain this.

?? Let’s say your simple task is to: List All The Files In All The Folders That A User Will Provide

The assigned task will involve creating a script to enumerate all files within user-specified folders. The procedural steps will encompass the following:

? ?? Importing the sys module and

? ?? Prompting users for inputs.

In executing it, you may encounter a scenario where a user provides two folders - one accurate and the other erroneous or non-existent.

When that happens, an error will be generated per our written code. (refer to the attached image).

.

.

?? DEVOPS ENGINEER’S RESPONSIBILITY ??

As a DevOps engineer, you should be able to handle such an error well so that your programme will not just terminate or continue with the error (unless you want it that way). This nuanced manoeuvring is accomplished through the adept use of exceptions.

???? In essence, exception handling is the mechanism that empowers a programme to furnish an appropriate error message to the end user in the event of an error or exception during programme runtime.

Simply put, your programme should be able to issue a proper error message to your user.

?? Use the “try” function and embed in it the part of the code likely to experience the error.

?? Then include the specific error message that should be displayed.

.

.

?? COMMON EXCEPTIONS TO CONSIDER: ??

Some common exceptions you should consider adopting are:

?? FileNotFoundError, PermissionError, NotADirectoryError and ZeroDivisionError.

Each addresses specific scenarios, enhancing the precision of error handling.

.

.

?? INTRODUCTION OF THE “BREAK” FUNCTION: ??

Additionally, you can introduce the “break” function. That will pause the programme during runtime and wait for the user to take the right action before permitting the programme to resume running.

On the other hand, the use of “continue” facilitates the seamless continuation of programme execution. That is to say, if you want it to continue running the programme despite the error, then you can use “continue” and not “break”.

.

.

?? PRO TIP ??

?? Prioritize Specificity in Exception Handling.

When implementing exception handling in your code, strive for specificity in catching and handling exceptions. Instead of resorting to broad catch-all statements, it is advisable to pinpoint exceptions at a granular level, tailored to the specific type of error anticipated.

That means, consider all the different scenarios that an error is likely to occur during the runtime of the programme, and introduce an exception in the code for each anticipated error.

.

.

?? FURTHER READING ??

? ? Python Official Documentation - Errors and Exceptions

? ? Effective Python: 90 Specific Ways to Write Better Python by Brett Slatkin


#devops #devopsjobs #devopslearning #DevOpsBestPractices #devopsengineer #python #pythoncode #pythondeveloper #pythonprogramming #AWS #awscloud #sre


Vishalakshi G

Lead I - Sales Support at UST | Expert in Sales Operations & Support | Enhancing Sales Processes & Client Relationships

1 年

Are you looking for Contract Staffing to your requirements?

回复
Fedir Kompaniiets

CEO & Co-Founder of Gart Solutions | Cloud Solutions Architect & Digital Transformation Consultant

1 年

Exception handling in Python ensures that your program can handle errors gracefully and communicate them effectively to the user. #devops #python #errorhandling #programming

Roman B.

Cloud Architect | Co-Founder & CTO at Gart Solutions | DevOps, Cloud & Digital Transformation

1 年

Exception handling is crucial in programming to ensure a smooth user experience in case any errors occur. ??

要查看或添加评论,请登录

Ing. David Twumasi-Minkah的更多文章

社区洞察

其他会员也浏览了