Terminates Java
Program
package com.myjava.exceptions;
public class myexception
{
public static void
main(string a[])
{
for(int
i=5;i>=0;i--)
{
system.out.println(10/i);
}
system.out.println("After
for loop...");
}
}
Java Exception Handling
package com.myjava.exceptions;
public class myexceptionhandle
{
public static void main(string a[])
{
try
{
for(int
i=5;i>=0;i--)
{
system.out.println(10/i);
}
}
catch(exception
ex)
{
system.out.println("Exception
Message: "+ex.getMessage());
ex.printstacktrace();
}
system.out.println("After
for loop...");
}
}
Throws Clause
package com.myjava.exceptions;
public class mythrowsclause
{
public static void
main(string a[])
{
mythrowsclause
mytc = new mythrowsclause();
try
{
for(int
i=0; i<5 i="" span="">5>
{
mytc.getjunk();
System.out.println(i);
}
}
catch (interruptedexception iex)
{
iex.printstacktrace();
}
}
public void getjunk()
throws interruptedexception
{
thread.sleep(1000);
}
}
throw
dause
package com.myjava.exceptions;
public class myexplicitthrow
{
public static void
main(string a[])
{
try
{
myexplicitthrow
met = new myexplicitthrow();
system.out.println("Length
of junk is "+met.getstringsize("junk"));
system.out.println("Length
of wrong is "+met.getstringsize("wrong"));
system.out.println("Length
of null string is "+met.getstringsize(null));
}
catch
(exception ex)
{
system.out.println("Exception
message: "+ex.getmessage());
}
}
public int getstringsize(string
str) throws exception
{
if(str
== null)
{
throw
new exception("String input is null");
}
return
str.length();
}
}
Multiple Cache Blocks
package com.myjava.exceptions;
import java.net.malformedurlexception;
import java.net.url;
public class mymultiplecatchblocks
{
public static void
main(string a[])
{
mymultiplecatchblocks
mmcb = new mymultiplecatchblocks();
mmcb.execute(1);
mmcb.execute(2);
}
public void
execute(int i)
{
try
{
if(i
== 1)
{
getintvalue("7u");
}
else
{
geturlobj("");
}
}
catch
(numberformatexception nfe)
{
system.out.println("Inside
NumberFormatException... "+nfe.getmessage());
}
catch
(malformedurlexception mue)
{
system.out.println("Inside
MalformedURLException... "+mue.getmessage());
}
catch
(exception ex)
{
system.out.println("Inside
Exception... "+ex.getmessage());
}
}
public int getintvalue(string
num)
{
return
integer.parseint(num);
}
public url
geturlobj(string urlstr) throws malformedurlexception
{
return
new url(urlstr);
}
}
No comments:
Post a Comment