e. Exception handling
An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions.
Advantages of exception handling:
- It is separates error handling code from "regular" code
- Propagating errors up the call stack (without tedious programming)
- Grouping error types and error differentiation
Categories of exceptions:
1. Checked exceptions: A checked exception is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. These are usually invalid conditions in area, which is not in reach of programmer. These exceptions cannot simply be ignored at the time of compilation. Some examples are missing files or database problems.
2. Runtime exceptions: A runtime exception is the programmer could have avoided an exception that occurs that probably. These reflect errors in programming logic.
3. Errors: These are not exceptions at all, but problems that arise beyond the control of the user or the programmer. Errors are typically ignored in your code because you can rarely do anything about an error. For example, if a stack overflow occurs, an error will arise. They are also ignored at the time of compilation.
Java uses try and catch keywords for exception handling. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following:
catch (ExceptionName e1)
An exception causes a jump to the end of try block. If the exception occurred in a method called from a try block, the called method is abandoned. If there's a catch block for the occurred exception or a parent class of the exception, the exception is now considered handled. At least one
'catch' block or one 'finally' block must accompany a 'try' statement. If all 3 blocks are present, the order is important. (try/catch/finally) finally and catch can come only with try, they cannot appear on their own.
Regardless of whether or not an exception occurred or whether or not it was handled, if there is a finally block, it'll be executed always. (Even if there is a return statement in try block).
If there was no exception or the exception was handled, execution continues at the statement after the try/catch/finally blocks.
3. Object oriented Concepts
a. Classes and objects
- Classes: A class is a structure that defines data and functions to operate/modify/work on that data.
- Objects :Object is a feature of a class, which is used for working on the particular properties of the class. A class can have any number of objects in memory at one time
- Names are used to access methods and variables.
- Different namespaces are used in java some of them are given below:
- Package names
- Type names
- Variable names
- Method names
- Local variable names (including parameters)
- Each declared Enums has its own namespace.
- The scope of a name is the part of a class in which it can be seen.
||Can be used
||Method where declaration is done
||Till method executes
||Only inside method where declaration is done
||Each object has a copy
||Inside a method, outside a class without static
||As long as object exists
||In all instance methods of the class
||Class where declaration is done
||Inside a method, outside a class with static
||As long as containing class is loaded
||In all methods of class
- The scope of a method declared anywhere in a class is the entire class.
- The scope of a method's formal parameters is the entire method.