🔹 1. Introduction
Error handling and type safety are core to developing
reliable software. Java is known for its strict exception model and nominal
typing, while TypeScript offers a more flexible approach with structural
typing and JavaScript-style error handling. This chapter breaks down
how both languages manage errors and how TypeScript’s typing system contrasts
with Java’s.
🔹 2. Exception Handling
Basics
✅ Java Exception Handling:
Java uses checked and unchecked exceptions with the
try-catch-finally structure.
try
{
int result = 10 / 0;
}
catch (ArithmeticException e) {
System.out.println("Error: " +
e.getMessage());
}
finally {
System.out.println("Cleanup
code");
}
✅ TypeScript Error Handling:
TypeScript also uses try-catch-finally, but all exceptions
are unchecked.
try
{
let result = 10 / 0;
}
catch (e) {
console.error("Error:", e);
}
finally {
console.log("Cleanup code");
}
|
Feature |
Java |
TypeScript |
|
Exception
types |
Checked &
unchecked |
All
exceptions are unchecked |
|
Required
handling |
Yes (for
checked exceptions) |
No |
|
Custom
exceptions |
Via
subclassing Exception |
By extending
Error |
|
Multiple
catch blocks |
✅
Supported |
✅
Supported (via instanceof) |
🔹 3. Checked vs.
Unchecked Exceptions (Java-only)
|
Type |
Examples |
Handling
Required? |
|
Checked |
IOException,
SQLException |
✅
Yes |
|
Unchecked |
NullPointerException,
RuntimeException |
❌
No |
✅ TypeScript has no concept of
checked exceptions.
🔹 4. Custom Error Types
✅ Java Custom Exception:
class
CustomException extends Exception {
public CustomException(String message) {
super(message);
}
}
✅ TypeScript Custom Error:
class
CustomError extends Error {
constructor(message: string) {
super(message);
this.name = "CustomError";
}
}
✅ Java requires throws
declaration for checked exceptions; TypeScript does not.
🔹 5. The finally Block
In both languages, finally is always executed, regardless of
success or failure:
finally
{
System.out.println("Closing
resources");
}
ts
CopyEdit
finally
{
console.log("Closing resources");
}
🔹 6. Structural vs.
Nominal Typing
✅ Java: Nominal Typing
Types must be declared and match by name or hierarchy.
interface
Animal {
void speak();
}
class
Dog implements Animal {
public void speak() {
System.out.println("Bark"); }
}
✅ TypeScript: Structural Typing
Any object with a matching structure satisfies the type.
interface
Animal {
speak(): void;
}
const
dog = {
speak: () =>
console.log("Bark"),
};
function
makeAnimalTalk(a: Animal) {
a.speak();
}
makeAnimalTalk(dog);
// ✅ No explicit `implements` needed
🔹 7. Duck Typing in
TypeScript
This behavior is sometimes called duck typing:
“If it walks like a duck and quacks like a duck, it’s a
duck.”
✅ Only the shape matters — not
the name.
🔹 8. Type Compatibility
Example
interface
Point {
x: number;
y: number;
}
let
p = { x: 1, y: 2, z: 3 }; // Extra properties allowed
let
q: Point = p; // ✅ Allowed due to structural match
✅ Java would throw an error
unless p explicitly implemented an interface.
🔹 9. Benefits &
Pitfalls
|
Concept |
Java |
TypeScript |
|
Error
granularity |
Precise and
verbose |
Simple,
lightweight |
|
Flexibility |
Less (due to
strict type system) |
More (due to
structural typing) |
|
Runtime
checking |
Full
exception support |
Only limited,
must be handled manually |
|
Type safety |
Enforced by
compiler and runtime |
Enforced only
at compile time |
🔹 10. Summary Table
|
Feature |
Java |
TypeScript |
|
Exception
Model |
Checked +
unchecked |
Unchecked
only |
|
Structural
Typing |
❌
Not supported |
✅
Yes |
|
Custom Error
Types |
extends
Exception |
extends Error |
|
Required
Error Handling |
✅
For checked exceptions |
❌
Not required |
|
Function Type
Checking |
Nominal |
Structural |
|
Extra
Properties in Objects |
❌
Not allowed |
✅
Allowed |
No — TypeScript is a superset of JavaScript. However, many of its features (like classes, interfaces, and typing) are inspired by Java and C#.
No. Java is compiled into bytecode for the JVM, whereas TypeScript compiles to JavaScript for browser or Node.js environments.
✅ Yes — both use interfaces for contracts, but TypeScript interfaces are erased at runtime.
Java is strictly statically typed. TypeScript adds optional static typing to JavaScript.
✅ Yes — syntactically very similar, but TypeScript compiles to JavaScript, so it's ultimately prototype-based under the hood.
Kind of — TypeScript supports method overload signatures, but only one implementation
✅ Yes — TypeScript uses implements for interfaces and extends for class inheritance.
✅ Yes — especially on the front-end (Angular), but for heavy-duty back-end logic, Java is still preferred.
Very similar — both allow defining reusable components and data structures with type constraints.
✅ Absolutely. It’s one of the most in-demand languages today, especially for full-stack roles.
Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.
Kindly log in to use this feature. We’ll take you to the login page automatically.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Comments(0)