Issues
Exceptions
- lit. An abnormal condition.
- An event that disrupts the normal flow of the program.
- An object which is thrown.
Declaration
Exception declaration is a way of letting the compiler and the programmer know, that a method can throw an exception in certain scenarios. It helps set clear expectations of the code behaviour and also warn the developer in case an possible exception is not caught.
Not all languages support this though. Some languages like Python and JavaScript are more lenient with the exceptions you throw. While some other languages will warn you (like ABAP) or even show an error (like Java) if you raise an exception which you have not declared.
Handling
Mechanism of handling exceptions so that the normal flow of the application can be maintained.
Propagation
An exception is first thrown from the top of the stack and if it is not caught, it drops down the call stack to the previous method,If not caught there, the exception again drops down to the previous method, and so on until they are caught or until they reach the very bottom of the call stack.This is called exception propagation.
đź’ˇ Rule: By default dev.issues.exception.types.uncheckeds are forwarded in calling chain (propagated).
class TestExceptionPropagation{
void m(){
int data=50/0;
}
void n(){
m();
}
void p(){
try{
n();
}
catch(Exception e){System.out.println("exception handled");}
}
public static void main(String args[]){
TestExceptionPropagation1 obj=new TestExceptionPropagation1();
obj.p();
System.out.println("normal flow...");
}
}
Output:exception handled
normal flow...
đź’ˇ Rule: By default, dev.issues.exception.types.checkeds are not forwarded in calling chain (propagated).
Types
Checked Exceptions
Checked at compile time — the ones you get warning for, to declare.
Unchecked Exceptions
Not checked at compile time - occur at run time.
No warning if you don't handle or specify the exception
Runtime error if not handled anywhere.
Common Unchecked Exceptions
Class Cast Exception
An exception occurs when a class which is not the superclass or which was not originally the superclass is casted.
For example, if Dog is casted to Cat. Or if Dog is upcasted to Animal and we try casting that Animal object to Cat.
It is advised to determine if the class you're downcasting is an instance of the class you're downcasting to.
Null Pointer Exception
Occurs when you try to access an object of a empty or null reference.
Custom Exceptions
User Defined Exception.
Why create custom exceptions?
-> To have your own exception and message - relevant to the application
Errors
- Errors are severe issues that typically occur due to serious problems outside the control of the program. They often lead to abnormal program behavior.
- Errors are irrecoverable.
Handling
- Catching: Generally not appropriate to catch errors. Can't be caught anyway.
- Then what? - Logging the error and exiting the program is recommended approach.
Common Errors
Out of memory
Common Causes
- Insufficient RAM: When your machine doesn’t have enough RAM (Random Access Memory) available, it struggles to process applications, execute programs, or access files. As a result, you encounter the dreaded “out of memory” error.
- Startup Programs: Certain tasks or applications automatically run in the background when you start your system. These are known as startup programs. If these programs consume a significant portion of your system resources (including RAM), you may encounter the “out of memory” error.
- Outdated Hardware: Older computers with limited resources (such as low RAM or slow processors) can’t handle today’s data or processing demands. Consequently, they may trigger errors like the “out of memory” issue.
- Heavy Tasks: Resource-intensive activities like playing demanding PC games, creating 3D models, or editing high-quality videos can exhaust your system’s memory capacity, leading to the error.
- Malware: Malicious software running in the background can consume system memory, leaving little room for intended applications to function. This can result in the “out of memory” error.
- Memory Leaks: When dev.memory management.leaks in os.memory.ram.user.heap go out of hand.
Stack Overflow
Occurs when os.memory.ram.user.stack overflows.
Common Causes
- Infinite Recursion: If a function calls itself without a proper termination condition, it can lead to an endless loop of function calls, eventually causing a stack overflow.
- Deep Call Stack: A function calling many other functions, or a series of functions calling each other in a long chain, can also exhaust the stack space.
- Large Local Variables: Allocating large data structures as local variables in functions can consume significant stack space.
- Excessive Function Arguments:Functions with a large number of arguments can increase the stack usage for each call.
Prevention
- Proper Termination Conditions: Ensure recursive functions have conditions that will eventually stop the recursion.
- Limit Recursion Depth: If recursion is necessary, limit the depth to avoid excessive stack use.
- Optimize Local Variables: Use variables judiciously within functions and consider storing large data structures on the heap instead.
- Review Function Calls: Analyze the call graph of your program to identify potential deep call stacks or unintended recursion.
Bugs
Bug or defect is a the diversion from the expected behaviour of a program or application. It is caused by mistakes while coding the logic.
Regression
A software bug in which a feature that has worked before stops working.
Bug Workflow
Bug Life Cycle Stages
- New/Open
- Deferred/Postponed
- Assigned
- In Progress
- Fixed
- Pending Retest
- Verified/Closed
- Reopened
These stages can vary according to the organization and team.
Failures
Issue in the end product (likely discovered by end-customer).
Code smell
Indicators of deeper underlying problems in the code.