python - Changing the default ValueError of file.index -


i using file.index search string in file.

def ifstringexistsinfile(self,path,linetextcheck):     file = open(path).read()     if file.index(linetextcheck):         print (linetextcheck + " found in: " + path)     else:         raise valueerror (linetextcheck + " not found in: " + path) 

my issue if not find string, automatically raises default valueerror , not go "else" code contains custom valueerror:

valueerror: substring not found 

is there way change default valueerror?

currently, way came wrap sentence "try except", so:

def ifstringexistsinfile(self,path,linetextcheck):     file = open(path).read()     try:         if file.index(linetextcheck):             print (linetextcheck + " found in: " + path)     except:             raise valueerror(linetextcheck + " not found in: " + path) 

any better way appreciated. thank in advance!

any better way appreciated

you solved how should solved.

note can create own exception creating class inherits baseexception, needed.


Comments

Popular posts from this blog

testing - Detect whether test has failed within fixture -

android - Create single AAR file from multiple modules using Gradle -

AbotX : How do you create a parallel crawler that stays on and can be added to at run time from new requests -