{"id":13464,"date":"2020-03-18T08:42:16","date_gmt":"2020-03-18T15:42:16","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=13464"},"modified":"2023-12-01T02:33:04","modified_gmt":"2023-12-01T10:33:04","slug":"java-throw-exception","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/","title":{"rendered":"How to Throw an Exception in Java"},"content":{"rendered":"\n<p>Exceptions are unexpected events that occur during program execution. When your code encounters an exception, the flow of your program will be terminated.<br><\/p>\n\n\n\n<p>It\u2019s important that you handle exceptions correctly when you\u2019re coding in Java. Otherwise, your code may terminate during execution and affect the end-user experience.<br><\/p>\n\n\n\n<p>That\u2019s where the throw and throws keywords come in. The Java throw and throws keywords are used to handle exceptions in Java. This tutorial will discuss, with reference to examples, how to use these keywords in Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Exceptions<\/h2>\n\n\n\n<p>In Java, there are two events that can impact the flow of your program: errors and exceptions.<br><\/p>\n\n\n\n<p>The first event is an error, which represents problems in Java Virtual Machine such as memory leaks, the system running out of memory, and library compatibility issues. When an error is encountered, a program is not likely to recover. However, errors are usually outside of the control of the coder, and so in Java, we do not handle errors.&nbsp;<br><\/p>\n\n\n\n<p>Exceptions are the second type of event that can impact your program. Exceptions can be caught and handled in your code. When an exception is encountered, an object is created with information about the exception, which you can then use to tell your code what should happen if an exception is encountered.<br><\/p>\n\n\n\n<p>Some reasons exceptions occur include:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Errors in your code<\/li>\n\n\n\n<li>Opening non-existent files<\/li>\n\n\n\n<li>Invalid user input<\/li>\n\n\n\n<li>Losing network connectivity<\/li>\n<\/ul>\n\n\n\n<p>There are two types of exceptions you should know about before you start handling exceptions:<br><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Unchecked exceptions are checked at run-time. These include <code>ArithmeticException<\/code> and <code>ArrayIndexOutOfBoundsException<\/code>.<\/li>\n\n\n\n<li>Checked exceptions are checked at compile-time, and include <code>IOException<\/code> and <code>InterruptedException<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>For the most part, you want to handle checked exceptions in your code. This is because unchecked exceptions are generally a result of programming errors, rather than unexpected behavior in your code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Throws Keyword<\/h2>\n\n\n\n<p>The Java <code>throws<\/code> keyword is used to declare the type of exceptions that could arise in a block of code. Here is the syntax for the throws clause:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>accessModifier return functionName() throws Exception1, Exception2 {\n   \/\/ Run code\n  }\n<\/pre><\/div>\n\n\n\n<p>Let\u2019s use an example to illustrate how this may work. Suppose we are building a program for a cinema that checks the age of a customer attending a movie for over 16s. If the customer is under the age of 16, they should not be allowed into the movie; if they are 16 or over, they should be permitted to enter.<br><\/p>\n\n\n\n<p>Here\u2019s the code we would use to check the age of the customer and throw an ArithmeticException exception if the customer is under the age of 16:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class Main {\n\tpublic static void verifyAge(int age) throws ArithmeticException {\n\t\tif (age &lt; 16) {\n\t\t\tthrow new ArithmeticException(\"This customer is not old enough to see the movie.\");\n\t\t} else {\n\t\t\tSystem.out.println(\"This customer is old enough to see the movie.\");\n\t\t}\n\t}\n\tpublic static void main(String[] args) {\n\t\tverifyAge(14);\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p>Exception in thread &#8220;main&#8221; java.lang.ArithmeticException: <code>This customer is not old enough to see the movie.<\/code><\/p>\n\n\n\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;at Main.verifyAge(Main.java:4)<\/code><\/p>\n\n\n\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;at Main.main(Main.java:11)<br><\/code><\/p>\n\n\n\n<p>When we run this program, the age parameter we specify in our main program is set to 14. This is less than 16, so when the <code>verifyAge() <\/code>method is executed, the statement <code>age &lt; 16 <\/code>evaluates to true, and an ArithmeticException is thrown.<br><\/p>\n\n\n\n<p>The throws keyword is used to define the type of exception that will be returned by the <code>verifyAge() <\/code>method. The throw keyword is used to throw the exception in our program.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Java Throw Keyword<\/h2>\n\n\n\n<p>The Java throw keyword is used to throw a single exception in your code. The throw keyword is followed by an object that will be thrown in the program if an exception is encountered.<br><\/p>\n\n\n\n<p>Here\u2019s the syntax for the Java throw keyword:<br><\/p>\n\n\n\n<p><code>throw throwObject;<br><\/code><\/p>\n\n\n\n<p>Let\u2019s walk through a few examples of the throw statement being used to handle exceptions in Java.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Throw Checked Exception<\/h2>\n\n\n\n<p>Suppose we want a message to appear that states, \u201cThis file does not exist.\u201d when an <code>IOException<\/code> is encountered in our program. <code>IOExceptions<\/code> are checked exceptions, and so we should handle them using the \u201cthrow\u201d keyword. Here\u2019s the code we would use to handle the exception:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>import java.io.*;\nclass Main\n\tpublic static void getFile() throws IOException {\n\t\tthrow new IOException(\"This file does not exist.\");\n\t}\n\t\n\tpublic static void main(String[] args) {\n\t\ttry {\n\t\t\tgetFile();\n\t\t} catch (IOException error) {\n\t\t\tSystem.out.println(e.getMessage());\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p>Our code returns:<br><\/p>\n\n\n\n<p><code>This file does not exist.<br><\/code><\/p>\n\n\n\n<p>Let\u2019s break down our code. First, we import the <code>java.io <\/code>library, which we would use in any program that handles files. Then we create a class called Main, which stores the code for our program.<br><\/p>\n\n\n\n<p>In our <code>Main<\/code> class, we define a function called <code>getFile() <\/code>which throws an <code>IOException<\/code>. This function will throw an IOException accompanied by the message \u201cThis file does not exist.\u201d to our program.<br><\/p>\n\n\n\n<p>In the main function in our Main class, we create a try\/catch block that tries to execute the <code>getFile()<\/code> method. If an IOException is encountered, the code within the catch block will execute, and the message from the IOException will be printed to the console. So, our code returns, \u201cThis file does not exist.\u201d, which is the message that we declared earlier.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Throw Unchecked Exception<\/h2>\n\n\n\n<p>So, suppose we wanted to throw a <code>StringIndexOutOfBounds<\/code> exception if our program tries to access an index value in a string that does not exist. We could do so using this code:<br><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>class Main {\n\tpublic static void outOfBounds() {\n\t\tthrow new StringIndexOutOfBoundsException(\"This index value is invalid.\");\n\t}\n\tpublic static void main(String[] args) {\n\t\toutOfBounds();\n\t}\n}\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>There\u2019s a lot going on in our code, so let\u2019s break it down. First, we have declared a class called Main in which our code for this program exists.<br><\/p>\n\n\n\n<p>Then we have created a function called <code>outOfBounds()<\/code> which, when executed, will throw a <code>StringIndexOutOfBounds<\/code> exception. The message <code>This index value is invalid.<\/code> will accompany the exception. Finally, in our main program, we call the <code>outOfBounds()<\/code> function, which then returns:<br><\/p>\n\n\n\n<p>Exception in thread &#8220;main&#8221; java.lang.StringIndexOutOfBoundsException: This index value is invalid.<\/p>\n\n\n\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;at Main.outOfBounds(Main.java:3)<\/code><\/p>\n\n\n\n<p><code>&nbsp;&nbsp;&nbsp;&nbsp;at Main.main(Main.java:7)<br><\/code><\/p>\n\n\n\n<p>In this example, we have thrown an unchecked exception. This is usually not necessary, as we discussed earlier, but we have thrown an unchecked exception anyway to show you the syntax.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Handling checked exceptions is an important part of writing maintainable code in Java. In Java, the throw and throws keywords are used to throw and handle exceptions.<br><\/p>\n\n\n\n<p>This tutorial covered how to use the throw and throws keywords in Java, and provided relevant examples to help you learn the process. After reading this tutorial, you are ready to start throwing and handling exceptions in Java like a pro!<\/p>\n","protected":false},"excerpt":{"rendered":"Exceptions are unexpected events that occur during program execution. When your code encounters an exception, the flow of your program will be terminated. It\u2019s important that you handle exceptions correctly when you\u2019re coding in Java. Otherwise, your code may terminate during execution and affect the end-user experience. That\u2019s where the throw and throws keywords come&hellip;","protected":false},"author":240,"featured_media":13466,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17289],"tags":[],"class_list":{"0":"post-13464","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Java","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":"","is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to Throw an Exception in Java: The Complete Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"java-throw-exceptionThe throw and throws keywords are used by Java coders to handle exceptions. Learn how to use these keywords in your code on Career Karma.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/java-throw-exception\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Throw an Exception in Java\" \/>\n<meta property=\"og:description\" content=\"java-throw-exceptionThe throw and throws keywords are used by Java coders to handle exceptions. Learn how to use these keywords in your code on Career Karma.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/java-throw-exception\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-18T15:42:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-01T10:33:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/woman-sitting-on-the-chair-using-laptop-2041629.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1000\" \/>\n\t<meta property=\"og:image:height\" content=\"667\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"James Gallagher\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Gallagher\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/\"},\"author\":{\"name\":\"James Gallagher\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"headline\":\"How to Throw an Exception in Java\",\"datePublished\":\"2020-03-18T15:42:16+00:00\",\"dateModified\":\"2023-12-01T10:33:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/\"},\"wordCount\":952,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/woman-sitting-on-the-chair-using-laptop-2041629.jpg\",\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/\",\"name\":\"How to Throw an Exception in Java: The Complete Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/woman-sitting-on-the-chair-using-laptop-2041629.jpg\",\"datePublished\":\"2020-03-18T15:42:16+00:00\",\"dateModified\":\"2023-12-01T10:33:04+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\"},\"description\":\"java-throw-exceptionThe throw and throws keywords are used by Java coders to handle exceptions. Learn how to use these keywords in your code on Career Karma.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/woman-sitting-on-the-chair-using-laptop-2041629.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/03\\\/woman-sitting-on-the-chair-using-laptop-2041629.jpg\",\"width\":1000,\"height\":667},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java-throw-exception\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/java\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to Throw an Exception in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e79364792443fbff794a144c67ec8e94\",\"name\":\"James Gallagher\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/01\\\/james-gallagher-150x150.jpg\",\"caption\":\"James Gallagher\"},\"description\":\"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/jamesgallagher\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to Throw an Exception in Java: The Complete Guide | Career Karma","description":"java-throw-exceptionThe throw and throws keywords are used by Java coders to handle exceptions. Learn how to use these keywords in your code on Career Karma.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/","og_locale":"en_US","og_type":"article","og_title":"How to Throw an Exception in Java","og_description":"java-throw-exceptionThe throw and throws keywords are used by Java coders to handle exceptions. Learn how to use these keywords in your code on Career Karma.","og_url":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-03-18T15:42:16+00:00","article_modified_time":"2023-12-01T10:33:04+00:00","og_image":[{"width":1000,"height":667,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/woman-sitting-on-the-chair-using-laptop-2041629.jpg","type":"image\/jpeg"}],"author":"James Gallagher","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"James Gallagher","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/"},"author":{"name":"James Gallagher","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"headline":"How to Throw an Exception in Java","datePublished":"2020-03-18T15:42:16+00:00","dateModified":"2023-12-01T10:33:04+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/"},"wordCount":952,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/woman-sitting-on-the-chair-using-laptop-2041629.jpg","articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/java-throw-exception\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/","url":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/","name":"How to Throw an Exception in Java: The Complete Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/woman-sitting-on-the-chair-using-laptop-2041629.jpg","datePublished":"2020-03-18T15:42:16+00:00","dateModified":"2023-12-01T10:33:04+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94"},"description":"java-throw-exceptionThe throw and throws keywords are used by Java coders to handle exceptions. Learn how to use these keywords in your code on Career Karma.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/java-throw-exception\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/woman-sitting-on-the-chair-using-laptop-2041629.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/03\/woman-sitting-on-the-chair-using-laptop-2041629.jpg","width":1000,"height":667},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/java-throw-exception\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/careerkarma.com\/blog\/java\/"},{"@type":"ListItem","position":3,"name":"How to Throw an Exception in Java"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e79364792443fbff794a144c67ec8e94","name":"James Gallagher","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/01\/james-gallagher-150x150.jpg","caption":"James Gallagher"},"description":"James Gallagher is a self-taught programmer and the technical content manager at Career Karma. He has experience in range of programming languages and extensive expertise in Python, HTML, CSS, and JavaScript. James has written hundreds of programming tutorials, and he frequently contributes to publications like Codecademy, Treehouse, Repl.it, Afrotech, and others.","url":"https:\/\/careerkarma.com\/blog\/author\/jamesgallagher\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/240"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=13464"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/13464\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/13466"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=13464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=13464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=13464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}