0% found this document useful (0 votes)
59 views27 pages

Radford Burger

This document provides guidance on handling exceptions when programming. It explains that exceptions should be caught using try-catch blocks, with the catch block specifying the exception type. When an exception occurs, the matching catch block will execute. Finally blocks will always execute whether an exception occurs or not, and are useful for cleaning up resources. The document stresses the importance of learning to properly handle exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views27 pages

Radford Burger

This document provides guidance on handling exceptions when programming. It explains that exceptions should be caught using try-catch blocks, with the catch block specifying the exception type. When an exception occurs, the matching catch block will execute. Finally blocks will always execute whether an exception occurs or not, and are useful for cleaning up resources. The document stresses the importance of learning to properly handle exceptions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

27 Mar 2023

Radford Burger
“Talk is cheap. Show me the code.”
— Linus Torvalds

•Exception-handling
Learning how to program
There will come a time while you develop projects
that you would feel stuck.
It could be anything from getting errors, your
program crashing without any message, or even
your coding executing fine but not generating the
output you desired.
You might get so restless sometimes that you might
want to give up.
What do you do in such scenarios?
Learning how to program
There will come a time while you develop projects
that you would feel stuck.
at e d
It could be anything from getting errors,
o t i v your
program crashing without any y
a m
message, or even
your coding executing fine ! S t
but not generating the
e u p
i
output you desired.
t g v
You mighto n ’
get so restless sometimes that you might
D
want to give up.
What do you do in such scenarios?
Learning how to program
Learn to Google the Error Correctly
This is a crucial step that you must master. Searching and surfing
the error of your code would help you correct your code within
few minutes but on the other hand, if are not sound at this skill it
would be like diving into a whirlpool of code without a map.

Tip: put the error generated by your compiler in double-quotes (“


”) before searching on Google. This way Google would
specifically target the error as the same sentence and that would
give a much accurate filtered result.
Learning how to program

Problem solving is an essential skill for


programming
As noted in (Gomes & Mendes, 2007a, 2007b), poor
problem solving ability frequently is the number one
cause of failure amongst programming students, which
suggests that being good at problem solving could be a
major predictor of success when learning programming.

You need to understand not only the syntax and


semantics of a language, but also how to systematically
break down a problem and then compose an algorithmic
solution - you need to study problem solving
Learning how to program
Embrace failure. “Analyze what you did wrong and use
it to correct yourself to do better in the future. Failures
are better teachers than successes because they cause
you to rethink your approach” (Oakley, 2014).

Learners should keep in mind that “we learn a good


deal from our failures. […] Know that you are making
progress with each mistake you catch when trying to
solve a problem - finding errors should give you a sense
of satisfaction.

Edison […] is said to have noted ‘I have not failed. I’ve


just found 10,000 ways that won’t work.’” (Oakley,
2014).
Learning how to program
“if you ask a dozen different people how to solve a
problem, you will probably receive 15 different answers.
Ask the Internet and you'll find dozens of listings…

The dilemma comes when you're faced


with all those reasonable-seeming approaches and
need to choose between them.
Learning how to program
But there is a big difference between reading through a code
example and being actually able to write the code on your own
and to run it successfully.

If you read through programming tutorials and books without


actually doing the hands-on examples on your own, you won’t
get much benefit out of your investment” (Kim & Harnish,
2012).
Learning programming “is a hands-on effort; watching
videos and solving multiple choice tests will not be
sufficient” (Staubitz, Klement, Teusner, Renz, & Meinel,
2016).
“Learn to focus on process, not product”
Learning how to program
(Norvig, 2001), who is director of research at Google,
states that “the best kind of learning is learning by
doing”.

In order to become good at programming, “the key is


deliberate practice: not just doing it again and again, but
challenging yourself with a task that is just beyond your
current ability, trying it, analyzing your performance
while and after doing it, and correcting any mistakes.

Then repeat. And repeat again.”


Learning how to program
what many programmers recommend to improve your
skills is to read and analyze other people’s code.
Athletes watch videos of practice sessions and
performances.
Chess players study games played by grand masters and
try to determine what the next move would be.
What was the other programmer thinking? What
caused him to choose this approach over another
one?
Learning how to program
It is important to note that practice problems naturally include a
solution, and it is tempting for learners to look at the solution
and reproduce it.

But “merely glancing at the solution to a problem and thinking


you know it yourself is one of the most common illusions of
competence in learning” (Oakley, 2014).

It should be avoided, as solving the problem on your


own “forces you to think your way through a problem
and provides a self-test of your understanding”.
Learning how to program
“Coding is a lot like riding a bike, until you put down the books,
the tutorials, and actually start doing it, it’s very hard for things to
“click.”

Even after you get on the bike and start pedalling, it takes a long
time before it feels natural.
The same thing goes with learning to programming: start
building stuff, start early, keep doing it.

It doesn’t matter if you don’t know how – the whole point is to


start building something, and then figure out how to do it along the
way.

It was the best way to practice and grow my skills” says well-known
programmer Eric Raymond (Chan, 2014).
Learning how to program
“You think you’ve set up everything the way you’re supposed to,
you’ve checked and re-checked it, and it still. doesn’t. work.
You don’t have a clue where to begin trying to fix it, and the error
message (if you’re lucky enough to have one at all) might as well
say ‘haibo.’
You might be tempted to give up at this point, thinking that you’ll
never figure it out, that you’re not cut out for programming.

It will happen to you as a beginner, but it will also happen to


you as an experienced programmer.
The main difference will be in how you respond to it”
(Carver, 2013)

“If you keep putting bricks on top of each other, it might


take a long time but eventually you’ll have a wall.
(Carver, 2013).
See example demonstrating Exception being thrown but
not handled:
class DivideByZeroNoExceptionHandling
A try block begins with the keyword try and is followed
by a block of code enclosed in curly braces.

We use the term “try block" to refer to the block of code


following the try keyword, as well as the keyword itself
A catch block begins with the keyword catch and is
followed by a parameter in parentheses (called the
exception parameter) and a block of code enclosed in
curly braces.

We use the term "catch block" to refer to the block of


code following the catch keyword, as well as the
keyword itself
See example demonstrating Exception handling:
class DivideByZeroWithExceptionHandling
At least one catch block or a finally block must
immediately follow the try block.
Each catch block specifies in parentheses an exception
parameter that identifies the exception type the handler
can process.
When an exception occurs in a try block, the catch
block that executes is the one whose type matches the
type of the exception that occurred (i.e., the type in the
catch block matches the thrown exception type exactly
or is a superclass of it.
If an exception occurs in a try block, the try block
terminates immediately and program control transfers to
the first of the following catch blocks in which the
exception parameter's type matches the type of the
thrown exception.
After the exception is handled, program control does
not return to the throw point because the try block has
expired (which also causes any of its local variables to
be lost).
After executing a catch block, this program's flow of
control proceeds to the first statement after the last
catch block, or…
to the statements in the finally block, if one has been
defined.

In fact, the statements in the finally block always


execute, whether an exception occurred or not.
Remember, a finally block is optional.
The finally block is a tool for preventing resource leaks.

When closing a file or connections or streams, place the


code in a finally block to ensure the resource is always
recovered.
This class is defined in java.util package, so an
appropriate import statement is required when handling
this type of exception.
Please check the API to see in which packages some of
the other common exception types are defined.
Example: ArithmeticException class is defined in
java.lang package, so does not require an import
statement.
• Complete Exception Prac on BB
• File-handling

You might also like