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

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -