0% found this document useful (0 votes)
3 views16 pages

Java String and Exception Handling Guide

This document discusses Strings in Java and exception handling. It defines a String as an immutable object that holds a sequence of characters. It describes how to create Strings and compares Strings. It then discusses exception handling in Java using try, catch, throw and throws. It differentiates between checked and unchecked exceptions and provides examples of each. Finally, it describes how to create user-defined exceptions by extending the Exception class.

Uploaded by

satyanarayana
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Java String and Exception Handling Guide

This document discusses Strings in Java and exception handling. It defines a String as an immutable object that holds a sequence of characters. It describes how to create Strings and compares Strings. It then discusses exception handling in Java using try, catch, throw and throws. It differentiates between checked and unchecked exceptions and provides examples of each. Finally, it describes how to create user-defined exceptions by extending the Exception class.

Uploaded by

satyanarayana
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT or read online on Scribd

Presentation by:

JOSHI
String
String
It is an Object of constant with group of characters. It
will not allow to modify as it is immutable. Every
modification creates a new String Object.
Creating string:
String s=null;
String s1=“Dear Friend”;
String s2=new String(“Dear Friend”);
String s3=s2;
String s4=new String(s2);
String s5=new String(char[ ], int, int);
String s6=new String(byte[ ], int, int);
String
• String1==String2
– compares references of two variables

• [Link](String2);
• [Link](String2);
– compares the values of two strings if
equal returns true else returns false
StringTokenizer
• An Object created on String to break a String
into tokens.
Constructors
StringTokenizer(String str)
StringTokenizer(String str, String delim)
StringTokenizer(String str, String delim,
boolean returnDelims)
while ([Link]())
{
println([Link]());
}
Exception Handling
PROGRAMMING
ERRORS

Syntax Errors / Logical Errors /


Compilation Runtime
Errors Errors
Exception
Exception Handling
• Exception is Logical Error/ Runtime Error
which abrupt the execution of application.
• Depending on provided values in the
statement, an exception may or may not
occur.
Ex: c = a / b ;

• If any statement is going throw an


Exception, it should be properly handled
to overcome the abruption of the
application.
try
• Try is block of statements which may
throw exceptions.
• The statements throws an exception
or exceptions raising must be there
in try block to handle it.
• One try block can have any number
of handlers.
catch
• Catch is an handler of exception
thrown from the try block.

• Catch should immediately follow the


try block.

• One try can have one catch or any


number of catches but they should
be continues.
try
{
statements;
}
catch(XXXException e)
{
handling statements;
}
catch(YYYException e)
{
handling statements;
}
throw
• It is used to throw an Object of Exception
type or any User Defined Exceptions.

• ‘throw’ must be used from try block only


to handle it properly.

Exception e=new Exception();


Throw e;
or
throw new Exception();
Exception
Checked Unchecked
Exception Exception

Examples: Examples:
IOException ArithmeticException
RemoteException FileNotFoundException
SocketException NumberFormatException
AllUserDefined ArrayIndexOutOfBounds
Exceptions Exception
throws
• ‘throws’ is used to specify Exception
in method declaration from where the
checked exceptions are thrown.

syntax:
returntype method(Args) throws CheckedException
{
statements;
}
User Defined Exceptions
• A class extended from Exception by
overriding the toString() method.

• These are used to raise an exception


on some conditions, restrictions,
validations etc.

• All the user defined exceptions are


Checked Exceptions which must be
specified with a ‘throws’ keyword.
class MyException extends Exception
{
properties;
constructor();
public String toString()
{
return Statement;
}
}

You might also like