· Hakan Çelik · Python · 5 dk okuma

Python'da Hata Yönetimi

Şimdiye kadar anlattığım konularda hiç hatalardan, onları nasıl yakalayabileceğimizden, ve gelen hataya göre nasıl işlemlerimizi devam ettirebileceğimizden hiç bahsetmemiştim ama bu konu oldukça öneml

Python’da Hata Yönetimi

Python’da Hata Yakalama

Şimdiye kadar anlattığım konularda hiç hatalardan, onları nasıl yakalayabileceğimizden, ve gelen hataya göre nasıl işlemlerimizi devam ettirebileceğimizden hiç bahsetmemiştim ama bu konu oldukça önemli bir konudur ve burda bunları öğreneceksiniz.

Size ufak bir örnek vereceğim

get_n = float(input("Bir sayı giriniz >> "))
result = 10 / get_n
print(f"sonuç {result}")

Böyle bir programda kullanıcı 1, 2 değilde 0 girerse ne olur ? hatalardan biri gerçekleşir ve programımız istediğimiz gibi bir sonuç vermez, siz burda 0 girme durumunu if ile kontrol edebilirdiniz veya bunu için python’da hata yakalama olayını kullanabilirsiniz.

Aşağıda gerçekleşen hatayı nasıl bulduğumuza dair bir script var.

try:
    1 / 0
except Exception as exc:
    print(f"Aman aman! {exc.__class__.__name__} hatasını yakaladık")

Çıktımız;

Aman aman! ZeroDivisionError hatasını yakaladık

Gördüğünüz gibi bir sayının 0 ile bölümünden aldığımız hata ZeroDivisionError’dır.

Belirli Hataları Yakalamak

Try, Except

Genel Yapısı

try:
   # bazı kodlarım
   pass
except ValueError:
   # ValueError hatası yakalanırsa çalışacak kodlarım
   pass
except (TypeError, ZeroDivisionError):
   # Birden fazla hata yakalama
   # TypeError ve ZeroDivisionError hataları gerçekleşirse çalışacak kodlarım
   pass
except Exception:
   # Yukarıdaki hatalar dışında başka bir hata gerçekleşirse çalışacak kodlarım
   pass

Try, Except, As

Burdaki as in amacı, yakaladığımız exception instance’ini bir isime atamaktır.

Genel Yapısı 1

try:
   # bazı kodlarım
   pass
except ValueError as error:
   # ValueError hatasu yakalanırsa error adında alıyorum ve print ediyorum
   print(error)

Genel Yapısı 2

try:
   # bazı kodlarım
   pass
except Exception as e:
   # herhangi bir hata yakalanırsa e adında alıyorum ve print ediyorum
   print(e)

Try, Except, Else

Genel Yapısı

try:
   # bazı kodlarım
   pass
except:
   # herhangi bir hata yakalanırsa çalışacak kodlarım
   pass
else:
    # hata olmaz ise çalışacak kodlarım.
    pass

Try, Except, Else, Finally

Genel Yapısı

try:
   # bazı kodlarım
   pass
except:
   # herhangi bir hata yakalanırsa çalışacak kodlarım
   pass
else:
    # hata olmaz ise çalışacak kodlarım.
    pass
finally:
    # hata olsada olmasada çalışacak kodlarım
    pass

Kendi Hata Sınıfımızı Oluşturup Hata Verelim

Bunu yapmak için Exception sınıfını ve raise keyword’ünü kullanacağız.

Raise

raise herhangi bir hata sınıfını ayağa kaldırmak için kullanılan bir keyword’dür.

Şimdi en başta verdiğimiz örneği tekrar yapalım.

class InputError(Exception):
    """İnput'daki hatalar için bir sınıf oluşturduk.

    Attributes:
        expression - hatanın oluştuğu girdi ifadesi
        message - hatanın açıklaması

        expression -- input expression in which the error occurred
        message -- explanation of the error
    """

    def __init__(self, expression, message):
        self.expression = expression
        self.message = message

input_ = float(input("0 hariç bir sayı girin >> "))
if input_ == 0:
    raise InputError(input_, "0 yazmayın")

Sonuç

Traceback (most recent call last):
  File "myapp.py", line 28, in <module>
    raise InputError(input_, "0 yazmayın")
__main__.InputError: (0.0, '0 yazmayın')

şimdi biraz daha güzelleştirelim ve dönderdiğimiz hatayıda yakalayım

class InputError(Exception):
    """İnput'dali hatalar için bir sınıf oluşturduk.

    Attributes:
        expression - hatanın oluştuğu girdi ifadesi
        message - hatanın açıklaması

        expression -- input expression in which the error occurred
        message -- explanation of the error
    """

    def __init__(self, expression, message):
        self.expression = expression
        self.message = message

def get_input():
    input_ = float(input("0 hariç bir sayı girin >> "))
    if input_ == 0:
        raise InputError(input_, "0 yazmayın")
    return input_

try:
    input_ = get_input()
except InputError as e: # dönen hata eğer bizim kodladığımız InputError ise
    print(e)
else:
    print(input_)

Sonuç

0 hariç bir sayı girin >> 0
(0.0, '0 yazmayın')

Python Built-in Exceptions

ExceptionCause of Error
AssertionErrorAssert ifadesi ( statement ) başarısız olduğunda yükselir ( raise eder ).
AttributeErrorÖzellik ataması veya referans başarısız olduğunda yükselir ( raise eder ).
EOFErrorİnput () fonksiyonu dosya sonu durumuna geldiğinde ortaya çıkar ( raise eder ).
FloatingPointErrorfloating-point işlemi başarısız olduğunda raise eder.
GeneratorExitGenerator’ün close() methodu çağrıldığında raise eder
ImportErrorİmport edilen modül bulunamadığında raise eder.
IndexErrorİndex dizi dışında olduğunda raise eder.
KeyErrorRaised when a key is not found in a dictionary.
KeyboardInterruptRaised when the user hits interrupt key (Ctrl+c or delete).
MemoryErrorRaised when an operation runs out of memory.
NameErrorRaised when a variable is not found in local or global scope.
NotImplementedErrorRaised by abstract methods.
OSErrorRaised when system operation causes system related error.
OverflowErrorRaised when result of an arithmetic operation is too large to be represented.
ReferenceErrorRaised when a weak reference proxy is used to access a garbage collected referent.
RuntimeErrorRaised when an error does not fall under any other category.
StopIterationRaised by next() function to indicate that there is no further item to be returned by iterator.
SyntaxErrorRaised by parser when syntax error is encountered.
IndentationErrorRaised when there is incorrect indentation.
TabErrorRaised when indentation consists of inconsistent tabs and spaces.
SystemErrorRaised when interpreter detects internal error.
SystemExitRaised by sys.exit() function.
TypeErrorRaised when a function or operation is applied to an object of incorrect type.
UnboundLocalErrorRaised when a reference is made to a local variable in a function or method, but no value has been bound to that variable.
UnicodeErrorRaised when a Unicode-related encoding or decoding error occurs.
UnicodeEncodeErrorRaised when a Unicode-related error occurs during encoding.
UnicodeDecodeErrorRaised when a Unicode-related error occurs during decoding.
UnicodeTranslateErrorRaised when a Unicode-related error occurs during translating.
ValueErrorRaised when a function gets argument of correct type but improper value.
ZeroDivisionErrorRaised when second operand of division or modulo operation is zero.

Aşağıdaki ağaç yapısında hangi hata nesnesinin hangi nesneden türetildiği açıkça görünüyor.

BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StopAsyncIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      |    +-- ModuleNotFoundError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      |    +-- RecursionError
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning

Yararlanılan Kaynaklar

Share:
Back to Blog

Related Posts

View All Posts »
Understanding Python Classes

Understanding Python Classes

Python · 2 dk

Python'da her şey nesnedir ve her nesnenin bir tipi vardır — primitifler, fonksiyonlar ve sınıfların kendisi de dahil. type() ve __class__ bu ilişkiyi ortaya çıkarır.

Run Methods Order In Python

Run Methods Order In Python

Python · 2 dk

Python metaclass'larında hangi metot ne zaman çalışır? Sınıf tanımı ve örnek oluşturma sırasındaki __prepare__, __new__, __init__, __call__ çalışma sırası.