PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A developer wants to raise a ValueError with a custom message when a negative number is passed. Which code is correct?
⚠ Common exam trap
The PCEP exam often tests the distinction between correct Python syntax for raising exceptions and common mistakes from other languages (like using `new` or omitting parentheses), so candidates must remember that Python uses `raise ExceptionClass("message")` without any extra keywords.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
raise ValueError("Negative input not allowed")
In Python, raising an exception with a custom message requires passing the message as an argument to the exception constructor: `raise ValueError("Negative input not allowed")`. This creates a ValueError instance with the specified message, which is the standard and only syntactically valid way to include a custom message in a raise statement.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
raise ValueError "Negative input not allowed"
Why it's wrong here
Python's raise statement requires an exception instance or class, so a bare string after ValueError is a syntax error; the message must be passed as an argument inside parentheses. Omitting parentheses is tempting because it reads naturally, and would be correct only if raise accepted a plain string, which it does not.
- ✗
raise ValueError() + "Negative input not allowed"
Why it's wrong here
Concatenating a string onto the ValueError instance with + raises TypeError, since exception objects do not support addition; the message belongs inside the parentheses. Appending text is tempting because it mirrors string building, and would be correct if the goal were concatenating two strings rather than raising an exception.
- ✓
raise ValueError("Negative input not allowed")
Why this is correct
The raise statement instantiates ValueError with the custom message string passed as its argument, immediately propagating the exception. This satisfies the requirement to raise ValueError with a custom message when a negative number is supplied, using correct Python exception-raising syntax.
- ✗
raise new ValueError("Negative input not allowed")
Why it's wrong here
Python's raise statement takes an exception instance or class directly; the keyword new belongs to languages like C# and Java, so this line is a SyntaxError before any ValueError is constructed. It tempts developers carrying over instantiation syntax, yet raise ValueError("Negative input not allowed") is the valid Python form.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.