
Exception 9. The throws clause
Exception 9. The throws clause
Some thrown exceptions require you to put a throws clause in the method signature. We talk about this.
Content
3.44 -> In this silly little program,
method "first" throws an exception
7.33 -> but does not catch it.
9.66 -> Since thrown exceptions
are checked,
11.76 -> the compiler will issue an error message
and refuse to compile the program.
17.67 -> We can get around this problem
19.16 -> by enclosing the throw statement
in a try statement,
22.62 -> but that is probably not
what we want.
25.329 -> Instead, we would like to indicate
27.46 -> that the calling method
should catch this exception.
30.68 -> To do this,
31.64 -> place a throws clause
in the header of the method.
35.7 -> The throws clause
has the form "throws,"
38.329 -> followed by a list
of class names separated by commas,
41.67 -> where each class name
is Throwable or one of its subclasses.
46.95 -> The occurrence of
such a throws clause
48.76 -> in the header of a method definition
50.99 -> relieves the method of the responsibility
of catching objects of the mentioned classes,
55.92 -> and places that burden
on any method that calls this one.
64.44 -> In the program
in the activity window,
66.84 -> method "main" is now responsible
for thrown exceptions.
70.48 -> It can relieve itself
of this responsibility
73.03 -> by having its own throws clause.
76.8 -> the Java runtime system
78.18 -> now has the responsibility
of catching exceptions
81.54 -> since it calls first,
and it will do so.
Source: https://www.youtube.com/watch?v=IjDL0tlywkM