Exception
Exception
I
l 1. Write a Java. program to demonstrate a division by zero exception
int numerator = 7;
int denominator= O;
try {
int answer = numerator I denominator;
System.out.println("Result: " + answer);
} catch (ArithmeticException ex) {
System.out.println("Error: "+ ex.getMessage());
System.out.println(''Cannot divide a value by zero.");
}
}
}. :
}
catch(Exception e)
{ .
System.out.println('' Thi~ is Arithmetic exception ");
} ~
}
Throw an. except" .f . . ·
older t ,, ion I age 1s below 18 (print "Access denied") . If age is 18 or
, , , pnn Access granted":
r-
pub l ic class Main J
I
if ~ age < 18 ) f
I, throw new Arit hm eticExce ptio n: "Access d(-riied - Y.:u !f,u:;; Cc- ,, t .h~.i ·.t
18 years old .");
else [
Sy stem . out . print l n ' "Access grc1nted ·· Yo u a,'~ old E21 ·Ju 1) : !
-,
j'
1
.I
,.
public stat ic void main '. String lJ args ) {
The throw statemen t is used together with an . exceptio n type. There are many
exception types available in
Java: Arithmet icExcep tion, Cla ssNotFoundEx ce pt ion , Arrayinde x0ut0fBo und sE
xception , Se curi tyEx ception, etc.
The exceptio n type is often used together with a custom method, like in the
example above.
. for a method
Used to throw an exception Used to indicate what
exception type may be thr
by a method own
I '.
I'
Syntax:
Syntax:
• throw s is followed by a
class
• and used with the
method signature
public class Rethrow {
public static void test1 () throws Exception {
, 'System.out.println("The Exception in test1 () method'J;
throw new Exception("thrown from test1 () method");
}
public static void test2() throws Throwab/e {
try {
, test1 ();
i: :} catch(Exception e) {
System.out.println("lnside test2() method");
throw e;
}
}
p~blic static void main(StringO args) throws Throwab/e {
I' try {
test2();
} catch(Exception e) {
System.out.println("Caught in main");
}
}
}
}
}
DI
( J\ '
9~ '7
Ii ~
~, ,. J~
uJ
~ ½ c,.V- lS
5
r
---....____~......,_u.-,.. -1,l...._ -p
::::0,-,-- .... . ,.
\
• . i
--- ~•.::c::::~ c.;:::.=::.--::-.c.-.=-:::=..i
I·
- - .. . . ...... . .
_ }_\! ck ___ ____ . ... . . _ __ • .
. -•- .. - .. _ ._ e1 ~ -J---~~- . . __E.~c1nm.obJec.J- .cr&d:o't:7
.. f.!,r.ep-J,ov,_ ___ . . -·.. . . ....
- - 1 ·_ii
i · ,.
j - - - = ~ •1,>~ ."'l.1.\.'i'li hJl ,X.,f',\
--4' ! Th.t. .+h.'4 bl~.ck. . -~ -~ OIi\€.. o'h ~e.v-e. .l')~iA- 1.~ +tto:1- ~--
' -
. \3~~ -~ -~~ep-h~, . . .-··
➔ [ 'J:,..f__~ ---cn1.e_._A:~ ~ dev...~ 0-V\. voc.e;;-h'm, ~e.. «tte.<M.u _1A} _ .- _ .. 1
•>-~~~- \V\ _--\\\._e_ 'o\.o~ ctAe ~kl'ppe.d 4- -~Cwh'd 'n j'u_~.S 7Q ·· _...
; c.~___\)_\:.i:>_t.k,,___~_J_ o -f \~c.ed V\ ~xi+ -b -f¼e. +""'j b\ o~k 1· . . . _
➔ · 1\f\ .. -~ ,PY08~- .:11,\~e.. l'\N1.~+ be... 0'11) ll o\f.\e _,h,'1 blocJc 4~qu../ ___,. · ___....-
~-~---~~~ __b_½,+__~e._~_h'.~ --- ~ - +~-- ,~.
po~\ ble. 11v... _:reu;ci_, . . . __ --······ . _..•
... . - ····---·- ··- -·- -·-- -- --- --~- ---. -- . . . . . ---- -·-
''I~'. ..A ~
. ...
--h\~u-~..d:efi~~~-d b'i -~t.. l~VJ-~oYd. :·C°'-¼ ~-(.~e,. o_v ~ - .. ~- _. ..;-- ····__ _.
. ~Y,term' ~ " ~ -~u!)_\I\ ~- _ 4.
\,'i ~\e_ ~"Y\j \)\1)u,l \..\.~\ue), ; t: °i' fY\J r,,,,'cd-e(~ ~--. .-
➔- T¼e.. ~ . \,\.o~k. 1~ ~ f\Mv.A.e.A1'a...te..~ o.,.{+~ .~ e. rj-Yj b_\pll(,__
➔ . .~t. c.o..+t.h . b\.otJ<.. +oo (p». \!\ewe.. Ov\e. . o't1 Vv\t,Y€., ~~~ - ~oJ- ,, .. -·-·
~\.e ~ ec.~-OS½f · +r, . f YJJ t€N'> . -:\lA. e.... -e..r:i t e.p+i M t _. . .
1. .
~ (_£.~c.yA'ovi -+y f e e. )
l . . .
1~ M:S ; II pvoltsS ~ A~t ..t ~c..o/>tt'o'-'\ ..
,,.- - ·Oi •····~ .• ,.)
W'H'#Jltvott::.-c:r.m-
r- ;.,::i NC .: 'S" 1
Cl.i'E ! I I
~- ·· --... ·~~.,·. - . -- -- ..
~.~~_lt- ... C:.)~~~ ·.·~~~D-~~. ·: ·... . - . . ....... ~~ -- ----~~
,:-:·· · : t f~\lL ..1'\a-¼ic. V!!\/, ~D.i~
\s,th~ -~s\:.1) .· -.
,.. . . ·.. ·· --- ...,l.....J.Ylit. C\ ~~ .~..\,~ 51.C.?- '5".., ..'},\J'j .'J.. . .... .. .. - --- .. -- ..... -
..... ·- .. . ...~:Y
·-1 ·~ ..... ~·· . . ... ... •· - - -- .,.-~. ,_.. , ~- ... . . . . . ·- · . . -· ..
.. .·.. . ... .. " . . ..... . '. ........ - t- .. .'y\ ~.9'. /..lb-C.J.., . .. --- JI. £~1.ff11.'M.. k&re.
.
. . .... ." .......... - .. . . \. I
·····- .... · ·· ·· · .. ....... ........ CA¼ .lJ\~,~w..t:h c.fo~.t~ph'oV1 . e._'} .---·-·· .... .. .. . . - 1
-: ........_- -.. .-... -· .1._ . . . . . . . . . .. . . " .. . . . .. . . . ... . .-............ --· - -·-··---· -..."
. . .. _......... :·1 ·--··-----·---· -- ·-- . ..... --- --- ------···---· - --------·--··--- ---·--
.---- .. .M u.1-li. pLe_ ..Co.AA ~~€A,·h7:" . ... .. __ . .. . ·--.... _ ...... . ...
. . ➔ . .T..+... 1s....pc.s>-i.b.!.e_ +v. IAo..ve.. ..."'r,tlo_'.Yf._:H.-t~ ....o.11tf.....~.ti~e.LA.f-.. 1
t Y1 . 711.e- ..
. ~~ ..~\ocx, --· -· .. --· -
~ e;oc.eph'<JYJ i\l\
_ W."'-.e.1/\. OJ.I\. _ .c,._ +'rt,- 1o,0c,~. . '.11.i ·ae.tt,.en.~1-1u..e.... .3~ --/-+1.~ ..
.fA.e.. .~+i'ple.. ~ / 4 ~ ~ ... \,Ke... wes:...IYL --~ .SW..~ ~ ~Vv\e~_
~ ~ _ ,A~~./:.~A:tcdUA-\v.rd- t4o/l.e. poo-0-,V.,j_elui -~akk~ .wl±h '\\A~ .~Xlc:f.f.±i.6-Yt .
. . Cl'o)e.c..+- _w)U.. ..he. f)Oe~JA- --1:tA.t. ~U,vt~'0.,.t\/'a·-- ~ ~ ~-W-l-\\ . _)..l-<:Jrpe.J..•.
< _ ~ 1.01/C,,. -~~:·-~or ..?-\~u.i-re... ~ r~.ce..v.Jf .d ...1¼L.f)Q_t_~n·OV\. cJ.- ~, .
4 ~e- et>vl(I ..A1W>fl7 ~~. <>... .c:.o:k\.l\ ~r\o.k\M.~ ..W11Yl .C½I\.
1
hlotl<- TQ _ .. ~7J
cwo,d - f>'t1Jd:r.c.>,.'!-':\ ...~ c ~.OY\ .1.... .... - --·· ....... ··- - ··-- --- -- --------·---·------ -- -- · .. . ... . -- · - . .
E.~.r>.w.ple..-. . c0-.\-cJ,,\ le.-,oc..e-p:h'oV\ _e..}',_.... //. tneM.wJ .'boc.~'ti~V) _ .
.. '.-: Tue.. ~c.½ 1:i+·cdt.,"fv\~. ~~wr~-.Ws . .wtfu:. Q\ .~~~l~\p\,\, IN"'''~ .
. cioeh 'V:)01¼ 1 ~ 1
• • • · .. . . • -· •
-- . - -l-n1
. \, ,'\/\~ ')'\.':. ~c~J / b.- o.C1J~-·J ·
c.ad-cJA ~ AYl¼ \M ~-\i' c.r~tepii'.oV\ 0
.i ~r¼M
otJ, pvl \/\ \V\ t~ ~l V\,,~~o 1/l ~l ~ IA) ., )
I
•
. . .• -·: ,. I
. '
...... . . . . . ~· -~· . . .-·-.
,
71li'1ov.l ~- :""Tl\e.~ 'YY.\"'-7' J,e.. ..1J'w,_f?I,)____~~ we.. . w0U-ld. \~ ke.. to --¼A.~ w .ou.y o~.V) ... l~
. ~c_T'm,s . • l,J e. __CAA-\ do ¾ 0.. . b'1- t.u.>; "?f '1'\I\ ~ K.tlj ~ 0 ,.-d .-\-½~ ~.. ·. •. ·.._-/ .; .
~ . s~ Vl~°" .-:- .... K~l.4:>0"rd _ obJ'~e-+- ~ d~ .+o ~e. -~'YUI.OV)
1' t
. . . . .. ~--roy.-:i . "'~ Tu~ _u'.lo..k>le.- bu.btlo.M> ·,
~ . UA u:I -1-n 11'\ ')1) \I\? -flt e___ -e.'PC epfi6Y) 'W\.CllVI ~ • . . .
--'> W e.._ - ~ . LU~ -fue.. ft.._ ')1J 1,4:) . "'-Vj 'f O'td 1V\ A/I""" i.~ .W ~ -
-"''r ~
"'r v-.uo
c'f \,'f . tx AA dJ c· t.'M f!f11 m clOV'v') 1.
o.r+.- N lL,,O I
~r.
. LI)
clru.1 _1::>tlMo
2 pv..b l1't, Ak4itc. VO ,'c~ w,o.h-1 l.S+-n~ t '))'-.
0,.
1';l1,:~:~~ w\d ~~~~s+,,•,
1 ~ . "' h'j .. - -~
1 flii,t,w ~ew ~0£.iot'7°-h'ovit)j 1 -lYlj . - ., . . -~
•
l 3'14¼M,oJ-,pr,'"'+wi \:1 Hnv.d \•,~ .. . . Co..+tl{(fh11V\W1..e.fi'c.fxc~t,·ovual!
4 Al\ .~e.n 9.)IJc.e.rr,(J"(\/2. ·fucd- C. \M~Od ~ 1'¼'>-0W ;\M\JA't \,e. <iecl~e.J '"' '1¼i.
'1V\ ~ W S d c~M • ~ ,f.. ~¼_ I?.~ ~ (?, \,\ \)~ I O'\ ~ iNv'f' le. "'."~ -M.e V1J10 t7 W I ll 't1 t1 tJ f.-•
~°"'¥\e ~- . c.\01.SS lh\.e'Mo
1 V'O ,\1 i\V\ow l ) .-\\f\')'l]Wl A'Y1+iA.w, e:Hct>0c.e,p1iov,
\ ~"' -ro~ \I\V--O A"fiH,w11 e+i'c..lKtcpfl.o~ •,
3-
J
) c\ON) t:>t..uAo
.
•
1_ pv..\;,\k Atod1t. vo\t\ 'VV\WV\ lS.¼cr ~1t)) ...
l Tu~e.\Mo o\:i~ "=-- 'Vle...w 'rv'\~e..wlol) ',
.\-~
~ obj,~ V\olA'.l l) 'i •
'J
WW\\_ A-r-/\\;...W\ '-:\~ t..t)oc~r,ti M ... o. e. .) .. .. . ..... .....
l 1~i.l¼iM, cu.t, p0 vith-'!.l<>.. e...)'., . . . . . :...... ..
~
·~
'
..
\-_
-_------.-=~~ ~ :_
~:i ~ -· --- -·- ·--·------
__//J,od>j
..
ol t,,} b\o<K _- . . -· - --
~_\o~k , t""~
. - . -·-- -~ _ t-3V\~.--\~.t~.....\6l \~ :.\)t '.(.'At'.~ci . ~ . {t_~~-1'~... J,lo c:Js,_ciaV..V'l11° ....
- - - - - - - - ----- ·-:~ ~ - O U \._b _~~-\:i(IYL.o~ . . ,-+: -t-Y'j ~,'IL fh:v1>w ¼t'-1 __
~~ -- j_+. wi11- be... .. _
- . --- -· ·
..
- _t)oe.tJ.JJ.e.d..,,_ . .,________- - --· - ··· ·- .. .. .. .
- - ,-- - _-,,,, _ Gi \'l.oJ.L'-f--""""- .l>L-"'>.J._ l"'
. .. o..~ ~-:h:Jsi....
'/ ·1
-
r
_=~·--·- ·--·----- . . . .
!. . _
--
. _
__
._
_ _
. _
.__
"-=_ . .... - - .. - .. ·-· ·- - .... ·-· · -
I 'b'-
/ ...
~
.
~
-- ·~--- 1· - ... ---
-- - :. --- - -- - - - --
-· . ---- - -
-- - . - - - ··· - -···-- ---- ~ --- ·~-~ ~ ;. . ; .\. J.~ ~~d;t ~. - . - ..
•· ·-· ·-----'Js .:.___.__ __·-· ............ ·•--...\. 1- ···-.. .. ___--
.. - --kV\~ ·-. --- - . . ·-
. ... --4-- A~~~-
--· ~ - -· -----·---- ---·---
. . .. ·---- - -
•
I
l
t-h y
~
1 e --:-• '/ !: ;
>j
g'j-tk ~...,. Qt,+•p¥?'n tl "' lt)
E_yro/'-.J.,.o,1 C\.t.)
Cd ·d .,t ( A-tt;-1£\\••t~ft•c_
t\) >} -
fI'\,
'5.'f-1.4<.l '-t •C'l,d-. ,F Y]t1. f l'-1...{
t , . ·····- -· .....
1'1 "'-'\.. l1
,+l "'- l~ t.f- t ~tc.u.~ d ") ;)
1 ) 1 \_-R,1."l • c 1.1.i-. p).;1.
Xf- \~ I l I
O
. -1
10'-' -
~1- d~ v- t.: ! E,'\C~i-
t ➔ . t.'a::.c.p't)·~-- t:r o.
➔ . 1+- i,~v- o\ v~ .fw o
.~ b' f ,-\,,e..
.64Ps,-
t¼ (J) -h?h
~(-=f11<f"'rl clG.Ss
~~~r o ~ u..~t•
~~
.;)..) ! ,.,_h~.,_,·;L l~~-k1.,d.\)
-h ,S t-n ~ l.) - d.~~v\ed
;V\ ~'>t'wo..b\e_ c.\aj,S,
h), vv·en'r1id e ~" -t
➔ _+. .S tn v_ ;l) ¼ 0..M.'+-o"--~~61-tt, h'\vo}-t6
bl ¼e.. s~)jttwi,o~-u.+hi-p~s)?..J'vrot/-/,i.,,.
f>1. . W i¼ .
6.:>.--.de - _ '>(J
"'1~~ou.+ C~-hr~-.:toh
ds f:..:~cep+iov,
.. c\e>,SS. u£..t:tJc.ep ~cu fuotepl-i'ovt
_c!o..55 \.4..~txtff> {~ kh
l ·,\,+- °' ~.. •
f"' lb li, S-h,·~ ~~"h,c
J l~ i
1 I 1.Ni'-~t¥{.i"'+ i ) .
~hi•,
h1
,ie.~11."'-- u E..-«.e.pi'oV! O{ct-1-<d
{
l a_ . -- i~ }
l)
p~JJli'c. .S +n '~
..,
J 1 .h~'tl.V\.k\.. _1, ~e.\\c:, C.\~s." •, j"
J
cl~_s IJ..!Je..v.d.oc.yi •
~.-a \Mo,.U., ~+-VI·~ ~ D ) ' d°"!>.s us.t.u..chX!c.7° ..
{_ pubh't ~~:tic. ( .$ ¾ '~ -~ D )
{ eubH _c. !> ~.c.. v.o.~d .Yvl~\11
.1 i. ~
l -- ~'1 1)\ /J 'l.< \W
v..~ex-c.tpU'1 t . .+ry . .... ~....... .. --~ ~a._e,o' c.epti-o);}
( -~~ '1e\.0
1.
.Cod-¼ -lu.~.J!~ce.p e.) .
~-\-c.~ l~~ce,p e)
.. f- ~-~~~\\1\-1)~-p"n\d\V\ t ~.) ;
....... . . -t~~=,.s~~ -~-~-t. p.'1\111ivi .LeJ;
) "
5
•
.. ' . .. .. ,, . .. ..
, '
- - .. •··
. - -- . -
' ' .. . ... . • .,, .. -
· ·· · .. ~' . .
,, . .. ... . .
, ·
.
. . ·- - ·-
J .. . . .. ·-
-1