PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Python · Theory

raise

← All stacks

Theory

64/268

raise

raise throws an error when your rule is broken. if age < 0: raise ValueError("age cannot be negative"). The caller can except it. Use a clear message. You can raise built-in types or a small custom class. This is how a function refuses bad input instead of returning a magic -1.

Tiny validation + raise ValueError is the fresher pattern. Don’t raise Exception("error") for everything — name the type.

Output — amount must be > 0. raise stops the function; except prints the message.

Diagram
try:  10 / 0
         │ ZeroDivisionError
         ▼
  except → Cannot divide
         │
         ▼
      finally
Exam tip

if age < 0: raise ValueError(...).

Example

# raise
def deposit(amt):
    if amt <= 0:
        raise ValueError("amount must be > 0")
    return amt

try:
    deposit(-10)
except ValueError as e:
    print(e)

Output: amount must be > 0. raise stops the function; except prints the message.

Short notes

  • Defraise Error("msg").
  • RuleValidate early.
  • RememberClear message.

Questions

1

Explain raise as if you are teaching a junior — definition, then one tiny script.

2

What does the example print, and why?

3

What mistake do freshers make with raise?

Previous← Multiple exceptionsNextfinally →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.