Seneca

Exceptions

Computer Science: OCR GCSE, topic 2.3.4

Exceptions

The program does not have to crash when an error happens. Exceptions give us a chance to handle the error more gracefully.

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.
Handling exceptions

Handling exceptions

  • If an exception can be dealt with internally, then we can catch the exception and continue with the program.
  • Catching an exception is done using a structure as below:
  • try: ---
  • except: ---
  • The code within the try block will be executed.
  • An exception may be thrown inside the try block.
  • Then the except block will be run to 'clean up' after the exception.

Test yourself on Exceptions

  • What is it called when an error arises in a program?

  • What is the purpose of the 'except' block of a try-except structure?

  • Select the true statement about exceptions.

  • What is the main difference between throwing an exception and catching an exception?

Practise this topic for free