4.3.1
Exceptions
Exceptions
Exceptions
Rather than leaving the program to crash when an error occurs, exceptions give us a chance to handle the error more gracefully.
![Illustrative background for Exceptions](https://image-v2.cdn.app.senecalearning.com/2018-08/71bff775-2bde-478f-a20c-e37e65a6a92a/exception-writing-pen-,h_400,q_80,w_640.jpg)
![Illustrative background for Exceptions ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/71bff775-2bde-478f-a20c-e37e65a6a92a/exception-writing-pen-,h_400,q_80,w_640.jpg)
Exceptions
Exceptions
- When an error arises in a program, the program is said to 'throw' an exception.
- An exception is a description of the error that arose.
- Exceptions can be 'caught' or handled rather than allowing the program to crash.
![Illustrative background for Handling exceptions](https://image-v2.cdn.app.senecalearning.com/2018-08/5d2afec2-aaf2-46a8-ab06-593c34f9d25d/Exception-writing-keyboard-,h_400,q_80,w_640.jpg)
![Illustrative background for Handling exceptions ?? "content](https://image-v2.cdn.app.senecalearning.com/2018-08/5d2afec2-aaf2-46a8-ab06-593c34f9d25d/Exception-writing-keyboard-,h_400,q_80,w_640.jpg)
Handling exceptions
Handling exceptions
- If an exception can be dealt with internally, then we can catch the exception and continue with the program.
- This is done using a structure as below:
- try: ---
- except: ---
- The code within the try block will be executed.
- If an exception is thrown inside the try block, then the except block will be run to 'clean up' after the exception.
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
Jump to other topics
1Introduction to Python
1.1Python
1.2Input & Output
1.3Data Types
1.4Variables & Constants
1.6Program Flow
2Data Structures
2.1Lists & Strings
2.2File Handling
3Modularity
3.1Subroutines
3.2Data Flow
4Good Practice
4.1Naming & Whitespace
4.2Comments
4.3Exceptions
![Go student ad image](/en-GB/revision-notes/_next/image?url=%2Fen-GB%2Frevision-notes%2Fimages%2Fgo-student-uk-ad.jpg&w=640&q=100)
Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered