Ana sayfa Genel Python – Exceptions (İstisnalar ve İstisna Yakalama)

Python – Exceptions (İstisnalar ve İstisna Yakalama)

4026
238
PAYLAŞ

Hatalar ve İstisnalar (Exceptions)

Bu yazıyla beraber artık Python programlarında oluşabilecek belli bir hatayı veya istisnayı nasıl çözeceğimizi öğrenmeye çalışacağız.

Hatalar

Python programlarında bazen bir değişkenin tanımlanmadan kullanılmaya çalıştırılması , bazen de yapılamayacak bir aritmetik işlemin yapılması Pythonda hatalara yol açar. Ancak bu istisnai durumlarda, hataların türüne göre programlarımızı daha güvenli bir şekilde yazabiliriz.Yani hata çıkarabilecek kodlarımızı öngörerek bu hataları programlarımızda yakalayabiliriz. Pythondaki bazı hatalara şunlar örnek verilebilir;

Bunun gibi Pythonda yüzlerce hata bulunur. Ancak bunların hiçbirini ezberlememize gerek yok. Kodumuzun durumuna göre zaten biz hataları çıktı olarak görebiliyoruz. Ama , siz yine de bunların neler olduğuna bakmak isterseniz Python’ın resmi sitesine bakabilirsiniz.

https://docs.python.org/3/library/exceptions.html

Hata Yakalama – try,except,finally

Bu konuda programlarımızda hata verebilecek kodları yakalayabildiğimiz try,except,finally bloklarını ve fonksiyonlarda kendi hatalarımızı nasıl fırlatacağımızı öğreneceğiz.

try , except blokları

try ,except bloklarının yapısı şu şekildedir;

Hemen try , except ile ilgili örneklerimize geçelim.

Örnek 1

Burada a = int(“324234sadsad”) kodu sıkıntı verdiği için program bu bloktan çıkarak direk except bloğuna girdi ve “Hata oluştu” yazdı. try ,except bloğu bitince program ekrana “Bloklar sona erdi” yazdırdı.

Örnek 2

Şimdi de 2 adet sıkıntı çıkaran kodumuz bulunsun. Birincisi ZeroDivisionError , diğeri ValueError hatası.

Şimdi de bu hataları yakalamaya çalışalım.

İstersek bu iki hatanın aynı except bloğuna girmesini sağlayabiliriz.

İşte burada gördüğümüz gibi bir kodda herhangi bir hata olduğunda try,except blokları ile hataları yakalayabiliyoruz.

try,except,finally blokları

Bazen programlarımızda her durumda mutlaka çalışmasını istediğimiz kodlar bulunabilir.Bunun için biz kendi try,except bloklarına ek olarak bir tane finally bloğu ekleyebiliriz. finally blokları hata olması veya olmaması durumunda mutlaka çalışacaktır. Yapısı şu şekildedir;

Hemen basit bir örnek yazalım.

Hata fırlatmak

Bazen kendi yazdığımız fonksiyonlar yanlış kullanılırsa kendi hatalarımızı üretip Pythonda bu hataları fırlatabiliriz. Bunun içinde raise anahtar kelimesini kullanacağız. Hata fırlatma şu şekilde yapılabilmektedir;

Bu fonksiyondan fırlatılan hatayı da try,except bloklarıyla yakalayabiliriz.

İşte Pythonda hata yakalama konusu bu kadar ! Artık hata yakalamayı kullanarak programlarımızı daha güvenli yazabiliriz.

238 YORUMLAR

  1. Hello There. I found your blog the usage of msn. That is a very smartly written article. I will make sure to bookmark it and come back to learn extra of your helpful info. Thank you for the post. I’ll definitely comeback.

  2. Simply wanna comment on few general things, The website design and style is perfect, the subject matter is rattling good. “I delight in men over seventy. They always offer one the devotion of a lifetime.” by Oscar Fingall O’Flahertie Wills Wilde.

  3. I like the valuable information you provide in your articles.
    I will bookmark your blog and check again here regularly.
    I am quite sure I will learn plenty of new stuff right here!
    Best of luck for the next!

  4. This blog is definitely rather handy since I’m at the moment creating an internet floral website – although I am only starting out therefore it’s really fairly small, nothing like this site. Can link to a few of the posts here as they are quite. Thanks much. Zoey Olsen

  5. I have been surfing online more than three hours these days, yet I never found any fascinating article like yours. It?¦s pretty value sufficient for me. In my opinion, if all website owners and bloggers made just right content as you probably did, the net can be much more useful than ever before.

  6. With havin so much content do you ever run into any problems of plagorism or copyright infringement? My website has a lot of exclusive content I’ve either created myself or outsourced but it seems a lot of it is popping it up all over the internet without my authorization. Do you know any techniques to help reduce content from being ripped off? I’d definitely appreciate it.

  7. We’re a group of volunteers and starting a new scheme in our community. Your site provided us with valuable info to work on. You have done a formidable job and our entire community will be thankful to you.

  8. Hi there! I know this is somewhat off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having difficulty finding one? Thanks a lot!

  9. Some truly wonderful articles on this site, thank you for contribution. “The difference between fiction and reality Fiction has to make sense.” by Tom Clancy.

  10. May I simply just say what a relief to uncover someone who genuinely
    understands what they’re talking about over the internet.
    You certainly know how to bring a problem
    to light and make it important. A lot more people should check this out and understand this side of the
    story. I was surprised you are not more popular since you definitely have the gift.

  11. You could certainly see your expertise in the paintings you write. The arena hopes for even more passionate writers like you who aren’t afraid to mention how they believe. At all times follow your heart.

  12. Hello my loved one! I want to say that this post is amazing, nice written and include almost all vital infos. I’d like to look more posts like this.

  13. hello!,I like your writing very much! share we communicate more about your article on AOL? I require an expert on this area to solve my problem. May be that’s you! Looking forward to see you.

  14. Thank you, I’ve recently been looking for info about this topic for ages and yours is the greatest I have discovered so far. But, what about the conclusion? Are you sure about the source?

  15. Thank you for sharing superb informations. Your web-site is so cool. I am impressed by the details that you¦ve on this site. It reveals how nicely you perceive this subject. Bookmarked this website page, will come back for extra articles. You, my friend, ROCK! I found simply the information I already searched all over the place and simply couldn’t come across. What a great website.

  16. My brother suggested I might like this web site. He used to be totally right. This post actually made my day. You cann’t consider just how much time I had spent for this information! Thanks!

  17. Hi there! I’m at work browsing your blog from my new iphone 4! Just wanted to say I love reading through your blog and look forward to all your posts! Keep up the excellent work!

  18. Excellent beat ! I wish to apprentice while you amend your site, how can i subscribe for a blog website? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea

  19. I simply couldn’t go away your site before suggesting that I actually enjoyed the usual info an individual supply to your visitors? Is gonna be again frequently in order to investigate cross-check new posts.

  20. Interesting blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple tweeks would really make my blog jump out. Please let me know where you got your theme. Thank you

  21. Today, I went to the beachfront with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

  22. 大部份曾經生育或經歷更年期的女性都會面臨私密深處及外觀鬆弛問題,不單會影響自信心和伴侶的親密體驗,更有機會造成容易積菌及尿滲等健康問題。全港首部 Venus Fiore閨密儀 RF射頻 私密緊緻療程 專門針對私密各處包括陰道、陰唇及陰阜的專用治療頭,全面改善鬆弛問題;每個治療頭均採用一次性即棄治療蓋,確保衛生。還有ATC自動控溫綜合系統,讓療程的治療溫度被有效監控,療程自然更安全、更有效達至預期效果。全港首部 Venus Fiore閨密儀 RF射頻 私密緊緻療程Venus Fiore集中為女性私密之處提供最到位的呵護,專注重整陰道健康、收緊鬆弛陰唇和改善陰阜肥大問題。關鍵在於Venus Concept的王牌─糅合多極射頻與磁力脈衝的(MP)2磁頻專利技術,發揮提升細胞活躍度、重塑同時增加膠原蛋白、刺激毛細血管新生等作用,達至調整陰道濕潤度、pH值的效果,更重要的是顯著改善陰道彈性,使其回復年輕狀態。

  23. Write more, thats all I have to say. Literally, it seems as though you relied on the video to make your point. You definitely know what youre talking about, why waste your intelligence on just posting videos to your weblog when you could be giving us something enlightening to read?

  24. Thanks , I have just been searching for info about this topic for ages and yours is the greatest I have discovered so far. But, what about the conclusion? Are you sure about the source?

  25. Hi there! Someone in my Facebook group shared this website with us so I came to take a look. I’m definitely enjoying the information. I’m book-marking and will be tweeting this to my followers! Great blog and amazing style and design.

  26. This is very interesting, You’re a very skilled blogger. I have joined your rss feed and look forward to seeking more of your magnificent post. Also, I have shared your website in my social networks!

  27. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  28. A person essentially help to make seriously posts I would state. This is the very first time I frequented your web page and thus far? I amazed with the research you made to make this particular publish incredible. Wonderful job!

  29. Thanks for sharing excellent informations. Your web site is very cool. I’m impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found just the info I already searched everywhere and just couldn’t come across. What a great web site.

  30. I would like to thank you for the efforts you have put in writing this blog. I am hoping the same high-grade site post from you in the upcoming as well. In fact your creative writing abilities has encouraged me to get my own website now. Actually the blogging is spreading its wings fast. Your write up is a great example of it.

  31. Hello There. I found your weblog the use of msn. That is a very smartly written article. I’ll make sure to bookmark it and return to read more of your useful info. Thanks for the post. I will certainly comeback.

  32. I really like your blog.. very nice colors & theme. Did you create this website yourself or did you hire someone to do it for you? Plz reply as I’m looking to construct my own blog and would like to know where u got this from. cheers

  33. Do you have a spam issue on this website; I also am a blogger, and I was wanting to know your situation; we have created some nice methods and we are looking to swap solutions with others, please shoot me an e-mail if interested.

  34. Wonderful beat ! I wish to apprentice while you amend your site, how could i subscribe for a blog website? The account helped me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear idea

  35. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your webpage? My website is in the very same niche as yours and my visitors would genuinely benefit from a lot of the information you present here. Please let me know if this okay with you. Cheers!

  36. Hey there I am so glad I found your webpage, I really found you by mistake, while I was researching on Digg for something else, Regardless I am here now and would just like to say cheers for a tremendous post and a all round interesting blog (I also love the theme/design), I don’t have time to read it all at the moment but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read more, Please do keep up the fantastic job.

  37. My programmer is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the expenses. But he’s tryiong none the less. I’ve been using Movable-type on a variety of websites for about a year and am worried about switching to another platform. I have heard excellent things about blogengine.net. Is there a way I can import all my wordpress posts into it? Any kind of help would be really appreciated!

  38. I have been browsing on-line more than 3 hours as of late, yet I never found any attention-grabbing article like yours. It’s beautiful value enough for me. In my view, if all webmasters and bloggers made just right content as you probably did, the internet will be much more helpful than ever before.

  39. Needed to draft you a tiny note to help say thanks a lot the moment again for your awesome pointers you have provided in this article. This is so seriously generous of people like you to offer openly exactly what many of us would have offered for sale as an e-book to generate some dough for themselves, mostly given that you might well have tried it in case you desired. These ideas also served as a fantastic way to comprehend other people online have a similar zeal much like my personal own to learn way more regarding this matter. I’m certain there are millions of more pleasant periods in the future for many who look into your site.

  40. Hey very nice web site!! Guy .. Beautiful .. Wonderful .. I will bookmark your web site and take the feeds additionally…I am happy to search out a lot of helpful information here within the put up, we’d like develop more techniques in this regard, thanks for sharing. . . . . .

  41. What¦s Taking place i’m new to this, I stumbled upon this I’ve discovered It positively useful and it has helped me out loads. I’m hoping to contribute & help other customers like its helped me. Good job.

  42. There are certainly a whole lot of details like that to take into consideration. That could be a great level to convey up. I provide the thoughts above as general inspiration but clearly there are questions just like the one you convey up where crucial thing can be working in sincere good faith. I don?t know if best practices have emerged around things like that, however I’m sure that your job is clearly identified as a good game. Each boys and girls feel the impact of only a moment’s pleasure, for the rest of their lives.

  43. The other day, while I was at work, my sister stole my iphone and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My iPad is now broken and she has 83 views. I know this is completely off topic but I had to share it with someone!

  44. It’s really a nice and helpful piece of info. I’m glad that you just shared this helpful info with us. Please keep us up to date like this. Thank you for sharing.

  45. What i don’t understood is actually how you’re no longer actually a lot more smartly-appreciated than you may be right now. You are very intelligent. You understand therefore considerably relating to this subject, made me individually consider it from numerous various angles. Its like women and men aren’t fascinated except it?¦s something to accomplish with Lady gaga! Your own stuffs great. All the time deal with it up!

  46. It is perfect time to make some plans for the long run and it is time to be happy. I have learn this put up and if I could I want to recommend you some attention-grabbing things or suggestions. Perhaps you could write subsequent articles regarding this article. I wish to learn more things about it!

  47. There are some fascinating closing dates in this article but I don’t know if I see all of them center to heart. There’s some validity however I will take hold opinion till I look into it further. Good article , thanks and we wish extra! Added to FeedBurner as well

  48. This design is incredible! You most certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Excellent job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  49. Do you have a spam problem on this blog; I also am a blogger, and I was curious about your situation; we have developed some nice practices and we are looking to exchange techniques with other folks, be sure to shoot me an e-mail if interested.

  50. Thanks, I have just been searching for information about this subject for ages and yours is the best I’ve came upon so far. But, what in regards to the bottom line? Are you certain concerning the source?

  51. I have been browsing online more than three hours as of late, but I never found any interesting article like yours. It’s lovely value enough for me. In my view, if all website owners and bloggers made just right content material as you probably did, the internet will probably be a lot more useful than ever before.

  52. You actually make it seem so easy with your presentation but I find this matter to be actually something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang of it!

  53. Thank you for another informative site. Where else could I get that type of information written in such a perfect way? I have a project that I am just now working on, and I have been on the look out for such info.

  54. When I originally commented I clicked the -Notify me when new comments are added- checkbox and now each time a comment is added I get four emails with the same comment. Is there any way you can remove me from that service? Thanks!

  55. I must show appreciation to you for bailing me out of this type of dilemma. As a result of checking through the the net and getting opinions which are not powerful, I thought my entire life was done. Living minus the solutions to the issues you have solved through your entire blog post is a crucial case, as well as the kind which could have in a wrong way affected my entire career if I had not come across your blog. Your primary ability and kindness in dealing with the whole thing was vital. I’m not sure what I would’ve done if I had not discovered such a subject like this. I can now look forward to my future. Thanks a lot very much for your impressive and results-oriented guide. I won’t hesitate to endorse your blog to any individual who should receive direction on this subject matter.

  56. I like the valuable info you provide to your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I’ll learn plenty of new stuff right here! Good luck for the following!

  57. Thanks for some other informative website. Where else may I am getting that kind of info written in such a perfect manner? I’ve a venture that I’m just now working on, and I’ve been on the glance out for such information.

  58. I needed to write you this little remark just to give thanks over again over the amazing views you’ve shown in this case. This is remarkably generous of you to present openly what many individuals might have advertised as an ebook to generate some bucks on their own, precisely considering the fact that you might well have done it if you ever considered necessary. Those thoughts as well worked to be a great way to be certain that other people online have the identical dreams the same as my very own to grasp more in respect of this issue. I know there are some more pleasurable occasions in the future for individuals who discover your blog post.

  59. Hi, i think that i saw you visited my web site so i came to “return the favor”.I am attempting to find things to enhance my web site!I suppose its ok to use some of your ideas!!

  60. Great tremendous issues here. I am very satisfied to look your post. Thanks a lot and i’m looking forward to touch you. Will you kindly drop me a mail?

  61. An impressive share, I just given this onto a colleague who was doing a little analysis on this. And he in fact bought me breakfast because I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you become expertise, would you mind updating your blog with more details? It is highly helpful for me. Big thumb up for this blog post!

  62. You could certainly see your enthusiasm within the paintings you write. The sector hopes for more passionate writers like you who are not afraid to mention how they believe. At all times go after your heart. “A second wife is hateful to the children of the first a viper is not more hateful.” by Euripides.

  63. Sosyal medya konusunda konusunda kesinlikle sizlere hizmetinden ödün vermeyerek hizmetlerini hızlandıran bir web sitesiyiz. Fiyat politikamızı siz müşterilerimiz için her zaman en uygun düzeyde tutmayı amaçlamaktayız. Siz değerli kullanıcılarımızın sitemizde daha fazla zaman geçirmesi ve bizden instagram takipçi satışı işlemini gerçekleştirmesi için hizmetlerimizin kalitesini sürekli olarak geliştirmekteyiz. Hahaha!

  64. It is appropriate time to make some plans for the future and it’s time to be happy. I have read this post and if I could I wish to suggest you some interesting things or suggestions. Perhaps you can write next articles referring to this article. I desire to read even more things about it!

  65. I think this is among the most important info for me. And i’m glad reading your article. But want to remark on few general things, The website style is wonderful, the articles is really excellent : D. Good job, cheers

  66. Thanks for another informative blog. Where else may I am getting that kind of info written in such a perfect way? I have a project that I’m simply now running on, and I have been on the glance out for such info.

  67. What’s Happening i am new to this, I stumbled upon this I’ve found It absolutely useful and it has aided me out loads. I hope to contribute & help other users like its helped me. Good job.

  68. I have recently started a website, the information you offer on this web site has helped me tremendously. Thank you for all of your time & work.

  69. Hmm it appears like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I wrote and say, I’m thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the whole thing. Do you have any points for first-time blog writers? I’d really appreciate it.

  70. Thanks for sharing excellent informations. Your web site is so cool. I’m impressed by the details that you have on this web site. It reveals how nicely you perceive this subject. Bookmarked this web page, will come back for extra articles. You, my pal, ROCK! I found just the information I already searched everywhere and simply couldn’t come across. What an ideal website.

  71. Oh my goodness! an incredible article dude. Thanks Nevertheless I’m experiencing difficulty with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting similar rss problem? Anybody who is aware of kindly respond. Thnkx

  72. I was recommended this web site through my cousin. I’m not certain whether or not this put up is written through him as nobody else recognise such certain approximately my difficulty. You’re amazing! Thanks!

  73. It?¦s actually a great and useful piece of information. I?¦m happy that you just shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

  74. Do you have a spam problem on this blog; I also am a blogger, and I was curious about your situation; we have created some nice practices and we are looking to swap strategies with others, please shoot me an e-mail if interested.

  75. Hi, I think your site might be having browser compatibility issues. When I look at your website in Safari, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, fantastic blog!

  76. Hi there this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I’m starting a blog soon but have no coding skills so I wanted to get guidance from someone with experience. Any help would be greatly appreciated!

  77. I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post…

  78. Good day! I could have sworn I’ve been to this website before but after checking through some of the post I realized
    it’s new to me. Anyways, I’m definitely delighted I found
    it and I’ll be book-marking and checking back often!

  79. I’m impressed, I have to admit. Seldom do I encounter a blog
    that’s equally educative and entertaining, and let me tell you, you have hit the nail on the head.
    The issue is something too few people are speaking intelligently
    about. I’m very happy that I found this during my search for
    something relating to this.

  80. Someone essentially assist to make seriously posts I
    might state. This is the first time I frequented
    your website page and up to now? I amazed with the research you made to make this
    particular post extraordinary. Great process!

  81. Hello there, I discovered your website by the use of Google
    whilst looking for a similar topic, your web site came up, it seems great.
    I have bookmarked it in my google bookmarks.
    Hello there, simply changed into aware of your weblog via Google, and located that
    it’s really informative. I’m going to be careful for brussels.
    I will be grateful for those who proceed this
    in future. A lot of other people will be benefited out of your writing.
    Cheers!

  82. I’m not that much of a online reader to be honest but your sites really nice, keep it up!
    I’ll go ahead and bookmark your website to come back later.
    Cheers

  83. Greetings from Florida! I’m bored at work so I decided to check out your blog on my iphone during lunch
    break. I love the information you present here
    and can’t wait to take a look when I get home.

    I’m amazed at how fast your blog loaded on my cell phone ..
    I’m not even using WIFI, just 3G .. Anyhow, amazing blog!

BİR CEVAP BIRAK

Please enter your comment!
Please enter your name here