Exception vs Event Handling

Hello, Folks! Today’s topic is exception and event handling. We will talk about keywords, mechanisms, and differences shortly.

Exception handling is an unexpected problem that happens during the execution of a program. With the help of this mechanism, the control from one part of the program can be passed to another. The process eases determination of error handling and the mistake in the actual functionality of the program.

If there are exceptions in your program, you can handle that with some provision of code which will take care of that exception. There are three main keywords in C++ to handle the exceptions; try, catch & throw. Some programming languages use raise instead of throw. C/C++ uses the word throw because, in the standard C library, there exists a function called raise unrelated to exception handling.

try — The block of code that may cause an exception is usually placed in the try block. When the exception occurs, it’s raised in the try block. Try block is followed by many catch blocks. Do not forget that every try block should have at leaast a corresponding catch block. A try block might have multiple catch blocks too.

catch — The catch block catches the block from the try block, and the code which handles the exception is written inside the current catch block.

throw — A program throws an exception when a problem occurs. This is done using a throw keyword.

Some programming languages have predefined exceptions, which are raised by the system implicitly such as Java. Unlike Java, C/C++ does not have such exceptions, the exceptions are only user-defined. All Java exceptions are objects of classes that are descendants of the Throwable class. There are two predefines subclasses of Throwable: Error and Exception. The Error class and its descendants are related to errors thrown by the Java run-time systems, such as running out of heap memory. There are two predefined subclasses of Exception: RuntimeException which is thrown when a user program causes an error. ArrayIndexOutOfBoundsException and NullPointerException are also descendants from RuntimeException. IOException is thrown when an error has occurred in an input or output operation. Exceptions of Error and RuntimeException and their descendants are called unchecked exceptions. All other exceptions are called checked exceptions.

in Java, there’s an additional finally clause, which is placed at the end of the list of handlers. If try clause throws no exception, finally is executed before executions, after the try construct. If try clause throws an exception and if it’s caught by the handler, the finally clause is executed after the handler completes its execution.

Image for post
Image for post

Error handling refers to the anticipation, detection, and resolution of programming, application, and communications errors. Specialized programs, called error handlers, are available for some applications.

Errors in the programs can happen in different phases. During the development, errors might occur due to syntax and logic. Syntax problems occur due to misspellings. It is possible to solve syntax problems by proofreading. Logic errors happen when the output of the executed code is not as expected. Logic errors are also called bugs. The solution to handling such errors are debugging programs.

During the execution of the program, run-time errors can happen because of hostile service or invalid input data. Lack of a satisfactory amount of memory or memory conflicts with another program can also cause run-time errors. Run-time errors can be resolved or their impact can be minimized.

I hope, you enjoyed and learned about the current topic. Don’t forget to read other articles of mine. Have fun, Peace ✌🏼

References

Master of Computer Science and Data Analytics at ADA/GW Universities, Researcher, Psychology-lover, Meme-maker, Musician, Writer, AI & Sarcasms!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store