Loading subject…
| Point of failure | Typical trigger | Java exception | Python exception |
|---|---|---|---|
| Unexpected input | letters typed where a number was expected | NumberFormatException | ValueError |
| Unexpected input | zero entered as a divisor | ArithmeticException | ZeroDivisionError |
| Resource unavailable | the named file does not exist | FileNotFoundException | FileNotFoundError |
| Logic error | a loop index runs past the last element | ArrayIndexOutOfBoundsException | IndexError |
| Logic error | a method called on an object that was never created | NullPointerException | AttributeError |
try block.catch block names one exception type and holds the code that runs if that type is thrown.Exception last if you use one at all.e holds the exception object, and e.getMessage() returns the detail the runtime attached to it.try and each handler except, and the behaviour matches Java's.except clauses can be listed, or one clause can name a tuple of types it will handle the same way.except ValueError as e binds the exception object so the message can be printed.else clause runs only when the try block finished without raising anything, which keeps the success path out of the guarded block.except: with no type catches everything, including a user pressing Ctrl+C, so name the types you mean.ValueError for int("abc") where Java raises NumberFormatException, so the names differ even though the situation is the same.finally block runs after the try block and after any handler, whether an exception was raised, whether it was caught, and even if the try block returned a value.finally as well as Java, so the block is available whichever language you sit.with open(...) as handle: closes the file for you, which is why it is preferred once you have understood what finally is doing.NumberFormatException when converting text to a number?finally block rather than at the end of the try block?import java.util.Scanner;
Scanner input = new Scanner(System.in);System.out.print("Enter a whole number: ");String entered = input.nextLine();
try { int number = Integer.parseInt(entered); int result = 100 / number; System.out.println("100 divided by " + number + " is " + result);} catch (NumberFormatException e) { System.out.println("That was not a whole number.");} catch (ArithmeticException e) { System.out.println("100 cannot be divided by zero.");}
System.out.println("Program continues.");entered = input("Enter a whole number: ")
try: number = int(entered) result = 100 / numberexcept ValueError: print("That was not a whole number.")except ZeroDivisionError: print("100 cannot be divided by zero.")else: print(f"100 divided by {number} is {result}")
print("Program continues.")FileReader reader = null;
try { reader = new FileReader("data.txt"); System.out.println((char) reader.read());} catch (IOException e) { System.out.println("data.txt could not be read.");} finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { System.out.println("The file could not be closed."); }}handle = None
try: handle = open("data.txt", "r") print(handle.readline())except FileNotFoundError: print("data.txt could not be read.")finally: if handle is not None: handle.close()