Nearby lessons

81 of 159

Python - Default Except Block

📌 What You Will Learn
  • Understand what the default except block does and when it runs
  • Write a default except block using the bare except: syntax
  • Apply the rule that the default except block must always be the last block
  • Explain why placing the default except block first raises a SyntaxError

Default except Block

We can use the default except block to handle any type of exception.

The default except block does not specify any exception type.

Generally, it is used to display a normal error message whenever an unexpected exception occurs.

Syntax

🐍Code Cell
1except:
2 statements
Output
No output captured.

Example

🐍Code Cell
1try:
2 x = int(input("Enter First Number: "))
3 y = int(input("Enter Second Number: "))
4 print(x/y)
5 
6except ZeroDivisionError:
7 print("ZeroDivisionError:Can't divide with zero")
8 
9except:
10 print("Default Except:Plz provide valid input only")
Output
No output captured.

Output 1 - ZeroDivisionError

D:\Python_classes>py test.py

Enter First Number: 10
Enter Second Number: 0

ZeroDivisionError:Can't divide with zero

Output 2 - Any Other Exception

D:\Python_classes>py test.py

Enter First Number: 10
Enter Second Number: ten

Default Except:Plz provide valid input only

Explanation

With the input 10 and 0, a ZeroDivisionError is raised, so the specific except ZeroDivisionError block runs.

With the input 10 and ten, a ValueError is raised. There is no ValueError handler, so Python executes the default except block.

Execution Flow

Program Starts
      │
      ▼
Execute try Block
      │
      ▼
Exception Raised
      │
      ▼
Matching Specific except ?
      │
 ┌────┴────┐
 │         │
 ▼         ▼
Yes        No
 │          │
 ▼          ▼
Execute   Execute
Specific  Default
except    except
 │          │
 └────┬─────┘
      ▼
Program Continues
      

Important Note

If a try block has multiple except blocks, then the default except block must always be the last block.

Otherwise, Python raises a SyntaxError.

Incorrect Example

🐍Code Cell
1try:
2 print(10/0)
3 
4except:
5 print("Default Except")
6 
7except ZeroDivisionError:
8 print("ZeroDivisionError")
Output
SyntaxError: default 'except:' must be last

Why Does this Error Occur?

The default except block can handle every type of exception.

If it is written before specific exception handlers, those handlers will never execute.

Therefore, Python requires the default except block to be placed last.

Various Possible Combinations of except Blocks

🐍Code Cell
1except ZeroDivisionError:
2 
3except ZeroDivisionError as msg:
4 
5except (ZeroDivisionError, ValueError):
6 
7except (ZeroDivisionError, ValueError) as msg:
8 
9except:
Output
No output captured.
📝 Key Takeaways
  • The default except block can handle any type of exception
  • It is written as a bare except: with no exception type
  • It runs only when no specific except block matches
  • The default except block must always be last, or Python raises a SyntaxError
  • It is commonly used to show a friendly error message for unexpected exceptions

🧠 Test Your Knowledge

6 Questions
Progress: 0 / 6