Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
240 views
in Computer by (49.6k points)
closed by

The raise Statement used.

1 Answer

+1 vote
by (51.9k points)
selected by
 
Best answer

The raise statement can be used to throw an exception. The syntax of raise statement is: 

raise exception-name [(optional argument)] 

The argument is generally a string that is displayed when the exception is raised. For example, when an exception is raised as shown in Figure. the message “OOPS : An Exception has occurred” is displayed along with a brief description of the error.

Use of the raise statement to throw an exception

The error detected may be a built-in exception or may be a user-defined one. Consider the example given in Figure. that uses the raise statement to raise a built-in exception called IndexError.

In Figure. since the value of variable length is greater than the length of the list numbers, an IndexError exception will be raised. The statement following the raise statement will not be executed. So the message “NO EXECUTION” will not be displayed in this case.

As we can see in Figure. in addition to the error message displayed, Python also displays a stack Traceback. This is a structured block of text that contains information about the sequence of function calls that have been made in the branch of execution of code in which the exception was raised. In Figure. the error has been encountered in the most recently called function that has been executed.

Use of raise statement with built-in exception

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...