Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

[python] exceptioneel

Pagina: 1
Acties:

  • Stijnb_
  • Registratie: Oktober 2009
  • Laatst online: 28-09 00:26
Beste Tweakers,

Ik ben nieuw in python en ben wat aan het experimenteren.
Ik had graag een exception ge triggerd die ik zelf heb gemaakt in de file waar ik mijn class in laad.
Maar ik wil de exception triggeren in de class en afhandelen in een andere bestand.

Ik vind het wat moeilijk om uit te leggen dus hier onder een voorbeeld:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import Bla

class CustomException(Exception):
    def __init__(self, value):
        self.parameter = value
    def __str__(self):
        return repr(self.parameter)

try:

  test = Bla()
  print 'test'

except CustomException, (instance):
    print "Caught: " + instance.parameter


Class:

code:
1
2
3
4
5
class Bla():

    def __init__(self):

        raise CustomException("My Useful Error Message")


Nu krijg ik dus de error dat "CustomException niet bestaat.
Maar het probleem is nu hoe krijg ik "CustomException" toch in mijn class?

Edit: kan iemand de titel wijzigen en exceptions komt door auto correct.

[ Voor 3% gewijzigd door Stijnb_ op 26-10-2014 17:59 ]


  • Naranya
  • Registratie: Oktober 2010
  • Laatst online: 20:30
Zet je exception in een aparte file en importeer die in de file waar je je class definieert?

exceptions.py
Python:
1
2
3
4
5
class CustomException(Exception):
    def __init__(self, value):
        self.parameter = value
    def __str__(self):
        return repr(self.parameter)


bla.py
Python:
1
2
3
4
5
6
7
8
9
10
11
from exceptions import CustomException

class Bla():
    def __init__(self):
        raise CustomException("My Useful Error Message")

try:
    test = Bla()
    print 'test'
except CustomException, (instance):
    print "Caught: " + instance.parameter

  • Stijnb_
  • Registratie: Oktober 2009
  • Laatst online: 28-09 00:26
Naranya schreef op zondag 26 oktober 2014 @ 18:51:
Zet je exception in een aparte file en importeer die in de file waar je je class definieert?

exceptions.py
Python:
1
2
3
4
5
class CustomException(Exception):
    def __init__(self, value):
        self.parameter = value
    def __str__(self):
        return repr(self.parameter)


bla.py
Python:
1
2
3
4
5
6
7
8
9
10
11
from exceptions import CustomException

class Bla():
    def __init__(self):
        raise CustomException("My Useful Error Message")

try:
    test = Bla()
    print 'test'
except CustomException, (instance):
    print "Caught: " + instance.parameter
Maar de class bla moet in een aparte file staan.
Want deze is groter dan een paar lijntjes.

  • Naranya
  • Registratie: Oktober 2010
  • Laatst online: 20:30
Dan doe je dat toch :)

exceptions.py
Python:
1
2
3
4
5
class CustomException(Exception):
    def __init__(self, value):
        self.parameter = value
    def __str__(self):
        return repr(self.parameter)


bla.py
Python:
1
2
3
4
5
from exceptions import CustomException

class Bla():
    def __init__(self):
        raise CustomException("My Useful Error Message")


main.py
Python:
1
2
3
4
5
6
7
8
from exceptions import CustomException
from bla import Bla

try:
    test = Bla()
    print 'test'
except CustomException, (instance):
    print "Caught: " + instance.parameter