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

Python – Exceptions (İstisnalar ve İstisna Yakalama)

1675
76
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.

76 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.

BİR CEVAP BIRAK

Lütfen yorumunuzu giriniz
Buraya isminizi giriniz