1
play

1 Handling exceptions (III) Handling exceptions (II) n try - PDF document

2.6 Error, exception and event Error and exception handling in handling Pascal n There are conditions that have to be fulfilled by a n While errors definitely can occur in Pascal programs, program that sometimes are not fulfilled, which Pascal


  1. 2.6 Error, exception and event Error and exception handling in handling Pascal n There are conditions that have to be fulfilled by a n While errors definitely can occur in Pascal programs, program that sometimes are not fulfilled, which Pascal does not allow to define special handling of causes a so-called program error. most errors n When an error occurs usually cannot be determined n The nearest we get to exception handling are some beforehand, otherwise checks could be already predicates that allow to check for very common included into the program conditions that might not be fulfilled and that can be used before statements are executed that will n Newer languages allow for a program to define produce an error certain errors that the program wants to handle on its own, using a so-called exception handler l eof n Similar techniques to the exception handler are used l <>nil to react to unexpected (and untimely) input (events) CPSC 449 Principles of Programming Languages Jörg Denzinger CPSC 449 Principles of Programming Languages Jörg Denzinger Error and exception handling in Java Error and exception handling in Java (I) (II) n All Java exceptions are objects of subclasses of the n Exception has yet another two subclasses, class Throwable . RuntimeException and IOException F Extension of the language n IOException is thrown, when an error occurred in an n Two of these subclasses are Error and Exception input or output operation, as defined in package java.io (see 2.7) n Error and its subclasses are related to errors thrown by the Java Virtual Machine and should never be n Java expects user programs to handle IOExceptions handled by a user program on their own l Example: StackOverflowError CPSC 449 Principles of Programming Languages Jörg Denzinger CPSC 449 Principles of Programming Languages Jörg Denzinger Error and exception handling in Java (III) Handling exceptions (I) n RuntimeException is thrown by the Java Virtual Machine n Java handles exceptions by using the try/catch/finally and it is up to the programmer if he/she wants to statement handle such an exception on his/her own or not n try is used to indicate a section (block) of code for n See java.lang.Exception for all the exceptions that are which the programmer wants to handle (some) already known to Java exceptions on his/her own n Users can define their own exceptions by adding n catch describes the handling of the exception it has as subclasses to the mentioned package argument n finally describes code that always should be executed after leaving the block defined by try , regardless how we left it CPSC 449 Principles of Programming Languages Jörg Denzinger CPSC 449 Principles of Programming Languages Jörg Denzinger 1

  2. Handling exceptions (III) Handling exceptions (II) n try -statements can be nested and naturally can include calls for methods from other objects Code block for which we want to catch try { n If an exception is thrown, where it is handled is … some exceptions determined from the inside out: } l If the inner try -statements has a catch -statement catch (SomeException e1) { that fits the exception, then this catch -statement is … Each catch deals with a class executed, the finally -statement is executed and the } of exceptions, determined by program continues after finally catch (AnotherException e2) { the run-time system based … l If the inner try -statement does not handle the on the type of the argument } exception, we continue outward and follow the finally { method call-stack The code in finally is executed always after … l If no catch -statement handles an exception, finally leaving the try-block } the run-time system will exit with an error CPSC 449 Principles of Programming Languages Jörg Denzinger CPSC 449 Principles of Programming Languages Jörg Denzinger Defining exceptions Using exceptions n By extending the Exception class or one of its n A method that does not handle an exception it throws subclasses has to announce this: public void myfunc(int arg) throws MyException { n Example: … class MyException extends Exception { } public MyException() { super(); } public MyException(String s) { super(s); } n To throw an user-defined exception within a block, we } use the throw command: throw new MyException("I always wanted to throw an exception!"); n Remember: exceptions are objects, therefore the new command! CPSC 449 Principles of Programming Languages Jörg Denzinger CPSC 449 Principles of Programming Languages Jörg Denzinger Events and their handling in Java (I) Events and their handling in Java (II) n Events are outside occurences that we want to have n For GUIs, the Java Swing package provides a an impact on our program collection of components that can be used for event handling n Most often events are related with what is happening n For each event class, it provides methods to add and in a graphical user interface (although events are also often occuring when you implement operating remove so-called event listeners that provide the systems or embedded systems) methods that are called if a certain event happens n Event handling is done similar to exception handling, n The event classes also have methods that allow for i.e. the outside event creates an object (resp. the run- analyzing the event more and naturally the listeners time system does this) and then the event handler for make use of these methods the particular event is executed CPSC 449 Principles of Programming Languages Jörg Denzinger CPSC 449 Principles of Programming Languages Jörg Denzinger 2

  3. Exception handling in imperative Exception handling in object- languages oriented languages n Exception handling is not a special ability of object- n All object-oriented languages come with a way of oriented languages, it can be included into exception handling, but Java is very object-oriented imperative languages, too in this regard, by having classes to define exception n C has exception handling capabilities, as has Ada objects and by dealing only with exception objects n The general ideas from Java, in more rudimentary n The messages-between-objects idea of object-oriented form (without using objects), apply also to programming allows exception handling to nicely fit imperative languages into the general concept n Most imperative languages are simply too old to n Java's strict typing of objects also makes exception have exception handling as part of their language, handling easier, by using it to determine which but often there were machine dependent extensions handler has to be evoked already capable of exception handling CPSC 449 Principles of Programming Languages Jörg Denzinger CPSC 449 Principles of Programming Languages Jörg Denzinger 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend